volumes.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/bio.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/random.h>
  24. #include <linux/iocontext.h>
  25. #include <linux/capability.h>
  26. #include <linux/ratelimit.h>
  27. #include <linux/kthread.h>
  28. #include "compat.h"
  29. #include "ctree.h"
  30. #include "extent_map.h"
  31. #include "disk-io.h"
  32. #include "transaction.h"
  33. #include "print-tree.h"
  34. #include "volumes.h"
  35. #include "async-thread.h"
  36. #include "check-integrity.h"
  37. #include "rcu-string.h"
  38. #include "math.h"
  39. #include "dev-replace.h"
  40. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  41. struct btrfs_root *root,
  42. struct btrfs_device *device);
  43. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  44. static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
  45. static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
  46. static DEFINE_MUTEX(uuid_mutex);
  47. static LIST_HEAD(fs_uuids);
  48. static void lock_chunks(struct btrfs_root *root)
  49. {
  50. mutex_lock(&root->fs_info->chunk_mutex);
  51. }
  52. static void unlock_chunks(struct btrfs_root *root)
  53. {
  54. mutex_unlock(&root->fs_info->chunk_mutex);
  55. }
  56. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  57. {
  58. struct btrfs_device *device;
  59. WARN_ON(fs_devices->opened);
  60. while (!list_empty(&fs_devices->devices)) {
  61. device = list_entry(fs_devices->devices.next,
  62. struct btrfs_device, dev_list);
  63. list_del(&device->dev_list);
  64. rcu_string_free(device->name);
  65. kfree(device);
  66. }
  67. kfree(fs_devices);
  68. }
  69. static void btrfs_kobject_uevent(struct block_device *bdev,
  70. enum kobject_action action)
  71. {
  72. int ret;
  73. ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
  74. if (ret)
  75. pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
  76. action,
  77. kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
  78. &disk_to_dev(bdev->bd_disk)->kobj);
  79. }
  80. void btrfs_cleanup_fs_uuids(void)
  81. {
  82. struct btrfs_fs_devices *fs_devices;
  83. while (!list_empty(&fs_uuids)) {
  84. fs_devices = list_entry(fs_uuids.next,
  85. struct btrfs_fs_devices, list);
  86. list_del(&fs_devices->list);
  87. free_fs_devices(fs_devices);
  88. }
  89. }
  90. static noinline struct btrfs_device *__find_device(struct list_head *head,
  91. u64 devid, u8 *uuid)
  92. {
  93. struct btrfs_device *dev;
  94. list_for_each_entry(dev, head, dev_list) {
  95. if (dev->devid == devid &&
  96. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  97. return dev;
  98. }
  99. }
  100. return NULL;
  101. }
  102. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  103. {
  104. struct btrfs_fs_devices *fs_devices;
  105. list_for_each_entry(fs_devices, &fs_uuids, list) {
  106. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  107. return fs_devices;
  108. }
  109. return NULL;
  110. }
  111. static int
  112. btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
  113. int flush, struct block_device **bdev,
  114. struct buffer_head **bh)
  115. {
  116. int ret;
  117. *bdev = blkdev_get_by_path(device_path, flags, holder);
  118. if (IS_ERR(*bdev)) {
  119. ret = PTR_ERR(*bdev);
  120. printk(KERN_INFO "btrfs: open %s failed\n", device_path);
  121. goto error;
  122. }
  123. if (flush)
  124. filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
  125. ret = set_blocksize(*bdev, 4096);
  126. if (ret) {
  127. blkdev_put(*bdev, flags);
  128. goto error;
  129. }
  130. invalidate_bdev(*bdev);
  131. *bh = btrfs_read_dev_super(*bdev);
  132. if (!*bh) {
  133. ret = -EINVAL;
  134. blkdev_put(*bdev, flags);
  135. goto error;
  136. }
  137. return 0;
  138. error:
  139. *bdev = NULL;
  140. *bh = NULL;
  141. return ret;
  142. }
  143. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  144. struct bio *head, struct bio *tail)
  145. {
  146. struct bio *old_head;
  147. old_head = pending_bios->head;
  148. pending_bios->head = head;
  149. if (pending_bios->tail)
  150. tail->bi_next = old_head;
  151. else
  152. pending_bios->tail = tail;
  153. }
  154. /*
  155. * we try to collect pending bios for a device so we don't get a large
  156. * number of procs sending bios down to the same device. This greatly
  157. * improves the schedulers ability to collect and merge the bios.
  158. *
  159. * But, it also turns into a long list of bios to process and that is sure
  160. * to eventually make the worker thread block. The solution here is to
  161. * make some progress and then put this work struct back at the end of
  162. * the list if the block device is congested. This way, multiple devices
  163. * can make progress from a single worker thread.
  164. */
  165. static noinline void run_scheduled_bios(struct btrfs_device *device)
  166. {
  167. struct bio *pending;
  168. struct backing_dev_info *bdi;
  169. struct btrfs_fs_info *fs_info;
  170. struct btrfs_pending_bios *pending_bios;
  171. struct bio *tail;
  172. struct bio *cur;
  173. int again = 0;
  174. unsigned long num_run;
  175. unsigned long batch_run = 0;
  176. unsigned long limit;
  177. unsigned long last_waited = 0;
  178. int force_reg = 0;
  179. int sync_pending = 0;
  180. struct blk_plug plug;
  181. /*
  182. * this function runs all the bios we've collected for
  183. * a particular device. We don't want to wander off to
  184. * another device without first sending all of these down.
  185. * So, setup a plug here and finish it off before we return
  186. */
  187. blk_start_plug(&plug);
  188. bdi = blk_get_backing_dev_info(device->bdev);
  189. fs_info = device->dev_root->fs_info;
  190. limit = btrfs_async_submit_limit(fs_info);
  191. limit = limit * 2 / 3;
  192. loop:
  193. spin_lock(&device->io_lock);
  194. loop_lock:
  195. num_run = 0;
  196. /* take all the bios off the list at once and process them
  197. * later on (without the lock held). But, remember the
  198. * tail and other pointers so the bios can be properly reinserted
  199. * into the list if we hit congestion
  200. */
  201. if (!force_reg && device->pending_sync_bios.head) {
  202. pending_bios = &device->pending_sync_bios;
  203. force_reg = 1;
  204. } else {
  205. pending_bios = &device->pending_bios;
  206. force_reg = 0;
  207. }
  208. pending = pending_bios->head;
  209. tail = pending_bios->tail;
  210. WARN_ON(pending && !tail);
  211. /*
  212. * if pending was null this time around, no bios need processing
  213. * at all and we can stop. Otherwise it'll loop back up again
  214. * and do an additional check so no bios are missed.
  215. *
  216. * device->running_pending is used to synchronize with the
  217. * schedule_bio code.
  218. */
  219. if (device->pending_sync_bios.head == NULL &&
  220. device->pending_bios.head == NULL) {
  221. again = 0;
  222. device->running_pending = 0;
  223. } else {
  224. again = 1;
  225. device->running_pending = 1;
  226. }
  227. pending_bios->head = NULL;
  228. pending_bios->tail = NULL;
  229. spin_unlock(&device->io_lock);
  230. while (pending) {
  231. rmb();
  232. /* we want to work on both lists, but do more bios on the
  233. * sync list than the regular list
  234. */
  235. if ((num_run > 32 &&
  236. pending_bios != &device->pending_sync_bios &&
  237. device->pending_sync_bios.head) ||
  238. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  239. device->pending_bios.head)) {
  240. spin_lock(&device->io_lock);
  241. requeue_list(pending_bios, pending, tail);
  242. goto loop_lock;
  243. }
  244. cur = pending;
  245. pending = pending->bi_next;
  246. cur->bi_next = NULL;
  247. if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
  248. waitqueue_active(&fs_info->async_submit_wait))
  249. wake_up(&fs_info->async_submit_wait);
  250. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  251. /*
  252. * if we're doing the sync list, record that our
  253. * plug has some sync requests on it
  254. *
  255. * If we're doing the regular list and there are
  256. * sync requests sitting around, unplug before
  257. * we add more
  258. */
  259. if (pending_bios == &device->pending_sync_bios) {
  260. sync_pending = 1;
  261. } else if (sync_pending) {
  262. blk_finish_plug(&plug);
  263. blk_start_plug(&plug);
  264. sync_pending = 0;
  265. }
  266. btrfsic_submit_bio(cur->bi_rw, cur);
  267. num_run++;
  268. batch_run++;
  269. if (need_resched())
  270. cond_resched();
  271. /*
  272. * we made progress, there is more work to do and the bdi
  273. * is now congested. Back off and let other work structs
  274. * run instead
  275. */
  276. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  277. fs_info->fs_devices->open_devices > 1) {
  278. struct io_context *ioc;
  279. ioc = current->io_context;
  280. /*
  281. * the main goal here is that we don't want to
  282. * block if we're going to be able to submit
  283. * more requests without blocking.
  284. *
  285. * This code does two great things, it pokes into
  286. * the elevator code from a filesystem _and_
  287. * it makes assumptions about how batching works.
  288. */
  289. if (ioc && ioc->nr_batch_requests > 0 &&
  290. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  291. (last_waited == 0 ||
  292. ioc->last_waited == last_waited)) {
  293. /*
  294. * we want to go through our batch of
  295. * requests and stop. So, we copy out
  296. * the ioc->last_waited time and test
  297. * against it before looping
  298. */
  299. last_waited = ioc->last_waited;
  300. if (need_resched())
  301. cond_resched();
  302. continue;
  303. }
  304. spin_lock(&device->io_lock);
  305. requeue_list(pending_bios, pending, tail);
  306. device->running_pending = 1;
  307. spin_unlock(&device->io_lock);
  308. btrfs_requeue_work(&device->work);
  309. goto done;
  310. }
  311. /* unplug every 64 requests just for good measure */
  312. if (batch_run % 64 == 0) {
  313. blk_finish_plug(&plug);
  314. blk_start_plug(&plug);
  315. sync_pending = 0;
  316. }
  317. }
  318. cond_resched();
  319. if (again)
  320. goto loop;
  321. spin_lock(&device->io_lock);
  322. if (device->pending_bios.head || device->pending_sync_bios.head)
  323. goto loop_lock;
  324. spin_unlock(&device->io_lock);
  325. done:
  326. blk_finish_plug(&plug);
  327. }
  328. static void pending_bios_fn(struct btrfs_work *work)
  329. {
  330. struct btrfs_device *device;
  331. device = container_of(work, struct btrfs_device, work);
  332. run_scheduled_bios(device);
  333. }
  334. static noinline int device_list_add(const char *path,
  335. struct btrfs_super_block *disk_super,
  336. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  337. {
  338. struct btrfs_device *device;
  339. struct btrfs_fs_devices *fs_devices;
  340. struct rcu_string *name;
  341. u64 found_transid = btrfs_super_generation(disk_super);
  342. fs_devices = find_fsid(disk_super->fsid);
  343. if (!fs_devices) {
  344. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  345. if (!fs_devices)
  346. return -ENOMEM;
  347. INIT_LIST_HEAD(&fs_devices->devices);
  348. INIT_LIST_HEAD(&fs_devices->alloc_list);
  349. list_add(&fs_devices->list, &fs_uuids);
  350. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  351. fs_devices->latest_devid = devid;
  352. fs_devices->latest_trans = found_transid;
  353. mutex_init(&fs_devices->device_list_mutex);
  354. device = NULL;
  355. } else {
  356. device = __find_device(&fs_devices->devices, devid,
  357. disk_super->dev_item.uuid);
  358. }
  359. if (!device) {
  360. if (fs_devices->opened)
  361. return -EBUSY;
  362. device = kzalloc(sizeof(*device), GFP_NOFS);
  363. if (!device) {
  364. /* we can safely leave the fs_devices entry around */
  365. return -ENOMEM;
  366. }
  367. device->devid = devid;
  368. device->dev_stats_valid = 0;
  369. device->work.func = pending_bios_fn;
  370. memcpy(device->uuid, disk_super->dev_item.uuid,
  371. BTRFS_UUID_SIZE);
  372. spin_lock_init(&device->io_lock);
  373. name = rcu_string_strdup(path, GFP_NOFS);
  374. if (!name) {
  375. kfree(device);
  376. return -ENOMEM;
  377. }
  378. rcu_assign_pointer(device->name, name);
  379. INIT_LIST_HEAD(&device->dev_alloc_list);
  380. /* init readahead state */
  381. spin_lock_init(&device->reada_lock);
  382. device->reada_curr_zone = NULL;
  383. atomic_set(&device->reada_in_flight, 0);
  384. device->reada_next = 0;
  385. INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
  386. INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
  387. mutex_lock(&fs_devices->device_list_mutex);
  388. list_add_rcu(&device->dev_list, &fs_devices->devices);
  389. mutex_unlock(&fs_devices->device_list_mutex);
  390. device->fs_devices = fs_devices;
  391. fs_devices->num_devices++;
  392. } else if (!device->name || strcmp(device->name->str, path)) {
  393. name = rcu_string_strdup(path, GFP_NOFS);
  394. if (!name)
  395. return -ENOMEM;
  396. rcu_string_free(device->name);
  397. rcu_assign_pointer(device->name, name);
  398. if (device->missing) {
  399. fs_devices->missing_devices--;
  400. device->missing = 0;
  401. }
  402. }
  403. if (found_transid > fs_devices->latest_trans) {
  404. fs_devices->latest_devid = devid;
  405. fs_devices->latest_trans = found_transid;
  406. }
  407. *fs_devices_ret = fs_devices;
  408. return 0;
  409. }
  410. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  411. {
  412. struct btrfs_fs_devices *fs_devices;
  413. struct btrfs_device *device;
  414. struct btrfs_device *orig_dev;
  415. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  416. if (!fs_devices)
  417. return ERR_PTR(-ENOMEM);
  418. INIT_LIST_HEAD(&fs_devices->devices);
  419. INIT_LIST_HEAD(&fs_devices->alloc_list);
  420. INIT_LIST_HEAD(&fs_devices->list);
  421. mutex_init(&fs_devices->device_list_mutex);
  422. fs_devices->latest_devid = orig->latest_devid;
  423. fs_devices->latest_trans = orig->latest_trans;
  424. fs_devices->total_devices = orig->total_devices;
  425. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  426. /* We have held the volume lock, it is safe to get the devices. */
  427. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  428. struct rcu_string *name;
  429. device = kzalloc(sizeof(*device), GFP_NOFS);
  430. if (!device)
  431. goto error;
  432. /*
  433. * This is ok to do without rcu read locked because we hold the
  434. * uuid mutex so nothing we touch in here is going to disappear.
  435. */
  436. name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
  437. if (!name) {
  438. kfree(device);
  439. goto error;
  440. }
  441. rcu_assign_pointer(device->name, name);
  442. device->devid = orig_dev->devid;
  443. device->work.func = pending_bios_fn;
  444. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  445. spin_lock_init(&device->io_lock);
  446. INIT_LIST_HEAD(&device->dev_list);
  447. INIT_LIST_HEAD(&device->dev_alloc_list);
  448. list_add(&device->dev_list, &fs_devices->devices);
  449. device->fs_devices = fs_devices;
  450. fs_devices->num_devices++;
  451. }
  452. return fs_devices;
  453. error:
  454. free_fs_devices(fs_devices);
  455. return ERR_PTR(-ENOMEM);
  456. }
  457. void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
  458. struct btrfs_fs_devices *fs_devices, int step)
  459. {
  460. struct btrfs_device *device, *next;
  461. struct block_device *latest_bdev = NULL;
  462. u64 latest_devid = 0;
  463. u64 latest_transid = 0;
  464. mutex_lock(&uuid_mutex);
  465. again:
  466. /* This is the initialized path, it is safe to release the devices. */
  467. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  468. if (device->in_fs_metadata) {
  469. if (!device->is_tgtdev_for_dev_replace &&
  470. (!latest_transid ||
  471. device->generation > latest_transid)) {
  472. latest_devid = device->devid;
  473. latest_transid = device->generation;
  474. latest_bdev = device->bdev;
  475. }
  476. continue;
  477. }
  478. if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
  479. /*
  480. * In the first step, keep the device which has
  481. * the correct fsid and the devid that is used
  482. * for the dev_replace procedure.
  483. * In the second step, the dev_replace state is
  484. * read from the device tree and it is known
  485. * whether the procedure is really active or
  486. * not, which means whether this device is
  487. * used or whether it should be removed.
  488. */
  489. if (step == 0 || device->is_tgtdev_for_dev_replace) {
  490. continue;
  491. }
  492. }
  493. if (device->bdev) {
  494. blkdev_put(device->bdev, device->mode);
  495. device->bdev = NULL;
  496. fs_devices->open_devices--;
  497. }
  498. if (device->writeable) {
  499. list_del_init(&device->dev_alloc_list);
  500. device->writeable = 0;
  501. if (!device->is_tgtdev_for_dev_replace)
  502. fs_devices->rw_devices--;
  503. }
  504. list_del_init(&device->dev_list);
  505. fs_devices->num_devices--;
  506. rcu_string_free(device->name);
  507. kfree(device);
  508. }
  509. if (fs_devices->seed) {
  510. fs_devices = fs_devices->seed;
  511. goto again;
  512. }
  513. fs_devices->latest_bdev = latest_bdev;
  514. fs_devices->latest_devid = latest_devid;
  515. fs_devices->latest_trans = latest_transid;
  516. mutex_unlock(&uuid_mutex);
  517. }
  518. static void __free_device(struct work_struct *work)
  519. {
  520. struct btrfs_device *device;
  521. device = container_of(work, struct btrfs_device, rcu_work);
  522. if (device->bdev)
  523. blkdev_put(device->bdev, device->mode);
  524. rcu_string_free(device->name);
  525. kfree(device);
  526. }
  527. static void free_device(struct rcu_head *head)
  528. {
  529. struct btrfs_device *device;
  530. device = container_of(head, struct btrfs_device, rcu);
  531. INIT_WORK(&device->rcu_work, __free_device);
  532. schedule_work(&device->rcu_work);
  533. }
  534. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  535. {
  536. struct btrfs_device *device;
  537. if (--fs_devices->opened > 0)
  538. return 0;
  539. mutex_lock(&fs_devices->device_list_mutex);
  540. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  541. struct btrfs_device *new_device;
  542. struct rcu_string *name;
  543. if (device->bdev)
  544. fs_devices->open_devices--;
  545. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  546. list_del_init(&device->dev_alloc_list);
  547. fs_devices->rw_devices--;
  548. }
  549. if (device->can_discard)
  550. fs_devices->num_can_discard--;
  551. new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
  552. BUG_ON(!new_device); /* -ENOMEM */
  553. memcpy(new_device, device, sizeof(*new_device));
  554. /* Safe because we are under uuid_mutex */
  555. if (device->name) {
  556. name = rcu_string_strdup(device->name->str, GFP_NOFS);
  557. BUG_ON(device->name && !name); /* -ENOMEM */
  558. rcu_assign_pointer(new_device->name, name);
  559. }
  560. new_device->bdev = NULL;
  561. new_device->writeable = 0;
  562. new_device->in_fs_metadata = 0;
  563. new_device->can_discard = 0;
  564. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  565. call_rcu(&device->rcu, free_device);
  566. }
  567. mutex_unlock(&fs_devices->device_list_mutex);
  568. WARN_ON(fs_devices->open_devices);
  569. WARN_ON(fs_devices->rw_devices);
  570. fs_devices->opened = 0;
  571. fs_devices->seeding = 0;
  572. return 0;
  573. }
  574. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  575. {
  576. struct btrfs_fs_devices *seed_devices = NULL;
  577. int ret;
  578. mutex_lock(&uuid_mutex);
  579. ret = __btrfs_close_devices(fs_devices);
  580. if (!fs_devices->opened) {
  581. seed_devices = fs_devices->seed;
  582. fs_devices->seed = NULL;
  583. }
  584. mutex_unlock(&uuid_mutex);
  585. while (seed_devices) {
  586. fs_devices = seed_devices;
  587. seed_devices = fs_devices->seed;
  588. __btrfs_close_devices(fs_devices);
  589. free_fs_devices(fs_devices);
  590. }
  591. return ret;
  592. }
  593. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  594. fmode_t flags, void *holder)
  595. {
  596. struct request_queue *q;
  597. struct block_device *bdev;
  598. struct list_head *head = &fs_devices->devices;
  599. struct btrfs_device *device;
  600. struct block_device *latest_bdev = NULL;
  601. struct buffer_head *bh;
  602. struct btrfs_super_block *disk_super;
  603. u64 latest_devid = 0;
  604. u64 latest_transid = 0;
  605. u64 devid;
  606. int seeding = 1;
  607. int ret = 0;
  608. flags |= FMODE_EXCL;
  609. list_for_each_entry(device, head, dev_list) {
  610. if (device->bdev)
  611. continue;
  612. if (!device->name)
  613. continue;
  614. ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
  615. &bdev, &bh);
  616. if (ret)
  617. continue;
  618. disk_super = (struct btrfs_super_block *)bh->b_data;
  619. devid = btrfs_stack_device_id(&disk_super->dev_item);
  620. if (devid != device->devid)
  621. goto error_brelse;
  622. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  623. BTRFS_UUID_SIZE))
  624. goto error_brelse;
  625. device->generation = btrfs_super_generation(disk_super);
  626. if (!latest_transid || device->generation > latest_transid) {
  627. latest_devid = devid;
  628. latest_transid = device->generation;
  629. latest_bdev = bdev;
  630. }
  631. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  632. device->writeable = 0;
  633. } else {
  634. device->writeable = !bdev_read_only(bdev);
  635. seeding = 0;
  636. }
  637. q = bdev_get_queue(bdev);
  638. if (blk_queue_discard(q)) {
  639. device->can_discard = 1;
  640. fs_devices->num_can_discard++;
  641. }
  642. device->bdev = bdev;
  643. device->in_fs_metadata = 0;
  644. device->mode = flags;
  645. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  646. fs_devices->rotating = 1;
  647. fs_devices->open_devices++;
  648. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  649. fs_devices->rw_devices++;
  650. list_add(&device->dev_alloc_list,
  651. &fs_devices->alloc_list);
  652. }
  653. brelse(bh);
  654. continue;
  655. error_brelse:
  656. brelse(bh);
  657. blkdev_put(bdev, flags);
  658. continue;
  659. }
  660. if (fs_devices->open_devices == 0) {
  661. ret = -EINVAL;
  662. goto out;
  663. }
  664. fs_devices->seeding = seeding;
  665. fs_devices->opened = 1;
  666. fs_devices->latest_bdev = latest_bdev;
  667. fs_devices->latest_devid = latest_devid;
  668. fs_devices->latest_trans = latest_transid;
  669. fs_devices->total_rw_bytes = 0;
  670. out:
  671. return ret;
  672. }
  673. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  674. fmode_t flags, void *holder)
  675. {
  676. int ret;
  677. mutex_lock(&uuid_mutex);
  678. if (fs_devices->opened) {
  679. fs_devices->opened++;
  680. ret = 0;
  681. } else {
  682. ret = __btrfs_open_devices(fs_devices, flags, holder);
  683. }
  684. mutex_unlock(&uuid_mutex);
  685. return ret;
  686. }
  687. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  688. struct btrfs_fs_devices **fs_devices_ret)
  689. {
  690. struct btrfs_super_block *disk_super;
  691. struct block_device *bdev;
  692. struct buffer_head *bh;
  693. int ret;
  694. u64 devid;
  695. u64 transid;
  696. u64 total_devices;
  697. flags |= FMODE_EXCL;
  698. mutex_lock(&uuid_mutex);
  699. ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
  700. if (ret)
  701. goto error;
  702. disk_super = (struct btrfs_super_block *)bh->b_data;
  703. devid = btrfs_stack_device_id(&disk_super->dev_item);
  704. transid = btrfs_super_generation(disk_super);
  705. total_devices = btrfs_super_num_devices(disk_super);
  706. if (disk_super->label[0]) {
  707. if (disk_super->label[BTRFS_LABEL_SIZE - 1])
  708. disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
  709. printk(KERN_INFO "device label %s ", disk_super->label);
  710. } else {
  711. printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
  712. }
  713. printk(KERN_CONT "devid %llu transid %llu %s\n",
  714. (unsigned long long)devid, (unsigned long long)transid, path);
  715. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  716. if (!ret && fs_devices_ret)
  717. (*fs_devices_ret)->total_devices = total_devices;
  718. brelse(bh);
  719. blkdev_put(bdev, flags);
  720. error:
  721. mutex_unlock(&uuid_mutex);
  722. return ret;
  723. }
  724. /* helper to account the used device space in the range */
  725. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  726. u64 end, u64 *length)
  727. {
  728. struct btrfs_key key;
  729. struct btrfs_root *root = device->dev_root;
  730. struct btrfs_dev_extent *dev_extent;
  731. struct btrfs_path *path;
  732. u64 extent_end;
  733. int ret;
  734. int slot;
  735. struct extent_buffer *l;
  736. *length = 0;
  737. if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
  738. return 0;
  739. path = btrfs_alloc_path();
  740. if (!path)
  741. return -ENOMEM;
  742. path->reada = 2;
  743. key.objectid = device->devid;
  744. key.offset = start;
  745. key.type = BTRFS_DEV_EXTENT_KEY;
  746. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  747. if (ret < 0)
  748. goto out;
  749. if (ret > 0) {
  750. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  751. if (ret < 0)
  752. goto out;
  753. }
  754. while (1) {
  755. l = path->nodes[0];
  756. slot = path->slots[0];
  757. if (slot >= btrfs_header_nritems(l)) {
  758. ret = btrfs_next_leaf(root, path);
  759. if (ret == 0)
  760. continue;
  761. if (ret < 0)
  762. goto out;
  763. break;
  764. }
  765. btrfs_item_key_to_cpu(l, &key, slot);
  766. if (key.objectid < device->devid)
  767. goto next;
  768. if (key.objectid > device->devid)
  769. break;
  770. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  771. goto next;
  772. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  773. extent_end = key.offset + btrfs_dev_extent_length(l,
  774. dev_extent);
  775. if (key.offset <= start && extent_end > end) {
  776. *length = end - start + 1;
  777. break;
  778. } else if (key.offset <= start && extent_end > start)
  779. *length += extent_end - start;
  780. else if (key.offset > start && extent_end <= end)
  781. *length += extent_end - key.offset;
  782. else if (key.offset > start && key.offset <= end) {
  783. *length += end - key.offset + 1;
  784. break;
  785. } else if (key.offset > end)
  786. break;
  787. next:
  788. path->slots[0]++;
  789. }
  790. ret = 0;
  791. out:
  792. btrfs_free_path(path);
  793. return ret;
  794. }
  795. /*
  796. * find_free_dev_extent - find free space in the specified device
  797. * @device: the device which we search the free space in
  798. * @num_bytes: the size of the free space that we need
  799. * @start: store the start of the free space.
  800. * @len: the size of the free space. that we find, or the size of the max
  801. * free space if we don't find suitable free space
  802. *
  803. * this uses a pretty simple search, the expectation is that it is
  804. * called very infrequently and that a given device has a small number
  805. * of extents
  806. *
  807. * @start is used to store the start of the free space if we find. But if we
  808. * don't find suitable free space, it will be used to store the start position
  809. * of the max free space.
  810. *
  811. * @len is used to store the size of the free space that we find.
  812. * But if we don't find suitable free space, it is used to store the size of
  813. * the max free space.
  814. */
  815. int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
  816. u64 *start, u64 *len)
  817. {
  818. struct btrfs_key key;
  819. struct btrfs_root *root = device->dev_root;
  820. struct btrfs_dev_extent *dev_extent;
  821. struct btrfs_path *path;
  822. u64 hole_size;
  823. u64 max_hole_start;
  824. u64 max_hole_size;
  825. u64 extent_end;
  826. u64 search_start;
  827. u64 search_end = device->total_bytes;
  828. int ret;
  829. int slot;
  830. struct extent_buffer *l;
  831. /* FIXME use last free of some kind */
  832. /* we don't want to overwrite the superblock on the drive,
  833. * so we make sure to start at an offset of at least 1MB
  834. */
  835. search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
  836. max_hole_start = search_start;
  837. max_hole_size = 0;
  838. hole_size = 0;
  839. if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
  840. ret = -ENOSPC;
  841. goto error;
  842. }
  843. path = btrfs_alloc_path();
  844. if (!path) {
  845. ret = -ENOMEM;
  846. goto error;
  847. }
  848. path->reada = 2;
  849. key.objectid = device->devid;
  850. key.offset = search_start;
  851. key.type = BTRFS_DEV_EXTENT_KEY;
  852. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  853. if (ret < 0)
  854. goto out;
  855. if (ret > 0) {
  856. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  857. if (ret < 0)
  858. goto out;
  859. }
  860. while (1) {
  861. l = path->nodes[0];
  862. slot = path->slots[0];
  863. if (slot >= btrfs_header_nritems(l)) {
  864. ret = btrfs_next_leaf(root, path);
  865. if (ret == 0)
  866. continue;
  867. if (ret < 0)
  868. goto out;
  869. break;
  870. }
  871. btrfs_item_key_to_cpu(l, &key, slot);
  872. if (key.objectid < device->devid)
  873. goto next;
  874. if (key.objectid > device->devid)
  875. break;
  876. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  877. goto next;
  878. if (key.offset > search_start) {
  879. hole_size = key.offset - search_start;
  880. if (hole_size > max_hole_size) {
  881. max_hole_start = search_start;
  882. max_hole_size = hole_size;
  883. }
  884. /*
  885. * If this free space is greater than which we need,
  886. * it must be the max free space that we have found
  887. * until now, so max_hole_start must point to the start
  888. * of this free space and the length of this free space
  889. * is stored in max_hole_size. Thus, we return
  890. * max_hole_start and max_hole_size and go back to the
  891. * caller.
  892. */
  893. if (hole_size >= num_bytes) {
  894. ret = 0;
  895. goto out;
  896. }
  897. }
  898. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  899. extent_end = key.offset + btrfs_dev_extent_length(l,
  900. dev_extent);
  901. if (extent_end > search_start)
  902. search_start = extent_end;
  903. next:
  904. path->slots[0]++;
  905. cond_resched();
  906. }
  907. /*
  908. * At this point, search_start should be the end of
  909. * allocated dev extents, and when shrinking the device,
  910. * search_end may be smaller than search_start.
  911. */
  912. if (search_end > search_start)
  913. hole_size = search_end - search_start;
  914. if (hole_size > max_hole_size) {
  915. max_hole_start = search_start;
  916. max_hole_size = hole_size;
  917. }
  918. /* See above. */
  919. if (hole_size < num_bytes)
  920. ret = -ENOSPC;
  921. else
  922. ret = 0;
  923. out:
  924. btrfs_free_path(path);
  925. error:
  926. *start = max_hole_start;
  927. if (len)
  928. *len = max_hole_size;
  929. return ret;
  930. }
  931. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  932. struct btrfs_device *device,
  933. u64 start)
  934. {
  935. int ret;
  936. struct btrfs_path *path;
  937. struct btrfs_root *root = device->dev_root;
  938. struct btrfs_key key;
  939. struct btrfs_key found_key;
  940. struct extent_buffer *leaf = NULL;
  941. struct btrfs_dev_extent *extent = NULL;
  942. path = btrfs_alloc_path();
  943. if (!path)
  944. return -ENOMEM;
  945. key.objectid = device->devid;
  946. key.offset = start;
  947. key.type = BTRFS_DEV_EXTENT_KEY;
  948. again:
  949. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  950. if (ret > 0) {
  951. ret = btrfs_previous_item(root, path, key.objectid,
  952. BTRFS_DEV_EXTENT_KEY);
  953. if (ret)
  954. goto out;
  955. leaf = path->nodes[0];
  956. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  957. extent = btrfs_item_ptr(leaf, path->slots[0],
  958. struct btrfs_dev_extent);
  959. BUG_ON(found_key.offset > start || found_key.offset +
  960. btrfs_dev_extent_length(leaf, extent) < start);
  961. key = found_key;
  962. btrfs_release_path(path);
  963. goto again;
  964. } else if (ret == 0) {
  965. leaf = path->nodes[0];
  966. extent = btrfs_item_ptr(leaf, path->slots[0],
  967. struct btrfs_dev_extent);
  968. } else {
  969. btrfs_error(root->fs_info, ret, "Slot search failed");
  970. goto out;
  971. }
  972. if (device->bytes_used > 0) {
  973. u64 len = btrfs_dev_extent_length(leaf, extent);
  974. device->bytes_used -= len;
  975. spin_lock(&root->fs_info->free_chunk_lock);
  976. root->fs_info->free_chunk_space += len;
  977. spin_unlock(&root->fs_info->free_chunk_lock);
  978. }
  979. ret = btrfs_del_item(trans, root, path);
  980. if (ret) {
  981. btrfs_error(root->fs_info, ret,
  982. "Failed to remove dev extent item");
  983. }
  984. out:
  985. btrfs_free_path(path);
  986. return ret;
  987. }
  988. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  989. struct btrfs_device *device,
  990. u64 chunk_tree, u64 chunk_objectid,
  991. u64 chunk_offset, u64 start, u64 num_bytes)
  992. {
  993. int ret;
  994. struct btrfs_path *path;
  995. struct btrfs_root *root = device->dev_root;
  996. struct btrfs_dev_extent *extent;
  997. struct extent_buffer *leaf;
  998. struct btrfs_key key;
  999. WARN_ON(!device->in_fs_metadata);
  1000. WARN_ON(device->is_tgtdev_for_dev_replace);
  1001. path = btrfs_alloc_path();
  1002. if (!path)
  1003. return -ENOMEM;
  1004. key.objectid = device->devid;
  1005. key.offset = start;
  1006. key.type = BTRFS_DEV_EXTENT_KEY;
  1007. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1008. sizeof(*extent));
  1009. if (ret)
  1010. goto out;
  1011. leaf = path->nodes[0];
  1012. extent = btrfs_item_ptr(leaf, path->slots[0],
  1013. struct btrfs_dev_extent);
  1014. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  1015. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  1016. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  1017. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  1018. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  1019. BTRFS_UUID_SIZE);
  1020. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  1021. btrfs_mark_buffer_dirty(leaf);
  1022. out:
  1023. btrfs_free_path(path);
  1024. return ret;
  1025. }
  1026. static noinline int find_next_chunk(struct btrfs_root *root,
  1027. u64 objectid, u64 *offset)
  1028. {
  1029. struct btrfs_path *path;
  1030. int ret;
  1031. struct btrfs_key key;
  1032. struct btrfs_chunk *chunk;
  1033. struct btrfs_key found_key;
  1034. path = btrfs_alloc_path();
  1035. if (!path)
  1036. return -ENOMEM;
  1037. key.objectid = objectid;
  1038. key.offset = (u64)-1;
  1039. key.type = BTRFS_CHUNK_ITEM_KEY;
  1040. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1041. if (ret < 0)
  1042. goto error;
  1043. BUG_ON(ret == 0); /* Corruption */
  1044. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  1045. if (ret) {
  1046. *offset = 0;
  1047. } else {
  1048. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1049. path->slots[0]);
  1050. if (found_key.objectid != objectid)
  1051. *offset = 0;
  1052. else {
  1053. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1054. struct btrfs_chunk);
  1055. *offset = found_key.offset +
  1056. btrfs_chunk_length(path->nodes[0], chunk);
  1057. }
  1058. }
  1059. ret = 0;
  1060. error:
  1061. btrfs_free_path(path);
  1062. return ret;
  1063. }
  1064. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  1065. {
  1066. int ret;
  1067. struct btrfs_key key;
  1068. struct btrfs_key found_key;
  1069. struct btrfs_path *path;
  1070. root = root->fs_info->chunk_root;
  1071. path = btrfs_alloc_path();
  1072. if (!path)
  1073. return -ENOMEM;
  1074. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1075. key.type = BTRFS_DEV_ITEM_KEY;
  1076. key.offset = (u64)-1;
  1077. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1078. if (ret < 0)
  1079. goto error;
  1080. BUG_ON(ret == 0); /* Corruption */
  1081. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  1082. BTRFS_DEV_ITEM_KEY);
  1083. if (ret) {
  1084. *objectid = 1;
  1085. } else {
  1086. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1087. path->slots[0]);
  1088. *objectid = found_key.offset + 1;
  1089. }
  1090. ret = 0;
  1091. error:
  1092. btrfs_free_path(path);
  1093. return ret;
  1094. }
  1095. /*
  1096. * the device information is stored in the chunk root
  1097. * the btrfs_device struct should be fully filled in
  1098. */
  1099. int btrfs_add_device(struct btrfs_trans_handle *trans,
  1100. struct btrfs_root *root,
  1101. struct btrfs_device *device)
  1102. {
  1103. int ret;
  1104. struct btrfs_path *path;
  1105. struct btrfs_dev_item *dev_item;
  1106. struct extent_buffer *leaf;
  1107. struct btrfs_key key;
  1108. unsigned long ptr;
  1109. root = root->fs_info->chunk_root;
  1110. path = btrfs_alloc_path();
  1111. if (!path)
  1112. return -ENOMEM;
  1113. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1114. key.type = BTRFS_DEV_ITEM_KEY;
  1115. key.offset = device->devid;
  1116. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1117. sizeof(*dev_item));
  1118. if (ret)
  1119. goto out;
  1120. leaf = path->nodes[0];
  1121. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1122. btrfs_set_device_id(leaf, dev_item, device->devid);
  1123. btrfs_set_device_generation(leaf, dev_item, 0);
  1124. btrfs_set_device_type(leaf, dev_item, device->type);
  1125. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1126. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1127. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1128. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1129. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1130. btrfs_set_device_group(leaf, dev_item, 0);
  1131. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1132. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1133. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1134. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1135. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1136. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  1137. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  1138. btrfs_mark_buffer_dirty(leaf);
  1139. ret = 0;
  1140. out:
  1141. btrfs_free_path(path);
  1142. return ret;
  1143. }
  1144. static int btrfs_rm_dev_item(struct btrfs_root *root,
  1145. struct btrfs_device *device)
  1146. {
  1147. int ret;
  1148. struct btrfs_path *path;
  1149. struct btrfs_key key;
  1150. struct btrfs_trans_handle *trans;
  1151. root = root->fs_info->chunk_root;
  1152. path = btrfs_alloc_path();
  1153. if (!path)
  1154. return -ENOMEM;
  1155. trans = btrfs_start_transaction(root, 0);
  1156. if (IS_ERR(trans)) {
  1157. btrfs_free_path(path);
  1158. return PTR_ERR(trans);
  1159. }
  1160. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1161. key.type = BTRFS_DEV_ITEM_KEY;
  1162. key.offset = device->devid;
  1163. lock_chunks(root);
  1164. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1165. if (ret < 0)
  1166. goto out;
  1167. if (ret > 0) {
  1168. ret = -ENOENT;
  1169. goto out;
  1170. }
  1171. ret = btrfs_del_item(trans, root, path);
  1172. if (ret)
  1173. goto out;
  1174. out:
  1175. btrfs_free_path(path);
  1176. unlock_chunks(root);
  1177. btrfs_commit_transaction(trans, root);
  1178. return ret;
  1179. }
  1180. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  1181. {
  1182. struct btrfs_device *device;
  1183. struct btrfs_device *next_device;
  1184. struct block_device *bdev;
  1185. struct buffer_head *bh = NULL;
  1186. struct btrfs_super_block *disk_super;
  1187. struct btrfs_fs_devices *cur_devices;
  1188. u64 all_avail;
  1189. u64 devid;
  1190. u64 num_devices;
  1191. u8 *dev_uuid;
  1192. int ret = 0;
  1193. bool clear_super = false;
  1194. mutex_lock(&uuid_mutex);
  1195. all_avail = root->fs_info->avail_data_alloc_bits |
  1196. root->fs_info->avail_system_alloc_bits |
  1197. root->fs_info->avail_metadata_alloc_bits;
  1198. num_devices = root->fs_info->fs_devices->num_devices;
  1199. btrfs_dev_replace_lock(&root->fs_info->dev_replace);
  1200. if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
  1201. WARN_ON(num_devices < 1);
  1202. num_devices--;
  1203. }
  1204. btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
  1205. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
  1206. printk(KERN_ERR "btrfs: unable to go below four devices "
  1207. "on raid10\n");
  1208. ret = -EINVAL;
  1209. goto out;
  1210. }
  1211. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
  1212. printk(KERN_ERR "btrfs: unable to go below two "
  1213. "devices on raid1\n");
  1214. ret = -EINVAL;
  1215. goto out;
  1216. }
  1217. if (strcmp(device_path, "missing") == 0) {
  1218. struct list_head *devices;
  1219. struct btrfs_device *tmp;
  1220. device = NULL;
  1221. devices = &root->fs_info->fs_devices->devices;
  1222. /*
  1223. * It is safe to read the devices since the volume_mutex
  1224. * is held.
  1225. */
  1226. list_for_each_entry(tmp, devices, dev_list) {
  1227. if (tmp->in_fs_metadata &&
  1228. !tmp->is_tgtdev_for_dev_replace &&
  1229. !tmp->bdev) {
  1230. device = tmp;
  1231. break;
  1232. }
  1233. }
  1234. bdev = NULL;
  1235. bh = NULL;
  1236. disk_super = NULL;
  1237. if (!device) {
  1238. printk(KERN_ERR "btrfs: no missing devices found to "
  1239. "remove\n");
  1240. goto out;
  1241. }
  1242. } else {
  1243. ret = btrfs_get_bdev_and_sb(device_path,
  1244. FMODE_WRITE | FMODE_EXCL,
  1245. root->fs_info->bdev_holder, 0,
  1246. &bdev, &bh);
  1247. if (ret)
  1248. goto out;
  1249. disk_super = (struct btrfs_super_block *)bh->b_data;
  1250. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1251. dev_uuid = disk_super->dev_item.uuid;
  1252. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1253. disk_super->fsid);
  1254. if (!device) {
  1255. ret = -ENOENT;
  1256. goto error_brelse;
  1257. }
  1258. }
  1259. if (device->is_tgtdev_for_dev_replace) {
  1260. pr_err("btrfs: unable to remove the dev_replace target dev\n");
  1261. ret = -EINVAL;
  1262. goto error_brelse;
  1263. }
  1264. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1265. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1266. "device\n");
  1267. ret = -EINVAL;
  1268. goto error_brelse;
  1269. }
  1270. if (device->writeable) {
  1271. lock_chunks(root);
  1272. list_del_init(&device->dev_alloc_list);
  1273. unlock_chunks(root);
  1274. root->fs_info->fs_devices->rw_devices--;
  1275. clear_super = true;
  1276. }
  1277. ret = btrfs_shrink_device(device, 0);
  1278. if (ret)
  1279. goto error_undo;
  1280. /*
  1281. * TODO: the superblock still includes this device in its num_devices
  1282. * counter although write_all_supers() is not locked out. This
  1283. * could give a filesystem state which requires a degraded mount.
  1284. */
  1285. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1286. if (ret)
  1287. goto error_undo;
  1288. spin_lock(&root->fs_info->free_chunk_lock);
  1289. root->fs_info->free_chunk_space = device->total_bytes -
  1290. device->bytes_used;
  1291. spin_unlock(&root->fs_info->free_chunk_lock);
  1292. device->in_fs_metadata = 0;
  1293. btrfs_scrub_cancel_dev(root->fs_info, device);
  1294. /*
  1295. * the device list mutex makes sure that we don't change
  1296. * the device list while someone else is writing out all
  1297. * the device supers.
  1298. */
  1299. cur_devices = device->fs_devices;
  1300. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1301. list_del_rcu(&device->dev_list);
  1302. device->fs_devices->num_devices--;
  1303. device->fs_devices->total_devices--;
  1304. if (device->missing)
  1305. root->fs_info->fs_devices->missing_devices--;
  1306. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1307. struct btrfs_device, dev_list);
  1308. if (device->bdev == root->fs_info->sb->s_bdev)
  1309. root->fs_info->sb->s_bdev = next_device->bdev;
  1310. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1311. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1312. if (device->bdev)
  1313. device->fs_devices->open_devices--;
  1314. call_rcu(&device->rcu, free_device);
  1315. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1316. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1317. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1318. if (cur_devices->open_devices == 0) {
  1319. struct btrfs_fs_devices *fs_devices;
  1320. fs_devices = root->fs_info->fs_devices;
  1321. while (fs_devices) {
  1322. if (fs_devices->seed == cur_devices)
  1323. break;
  1324. fs_devices = fs_devices->seed;
  1325. }
  1326. fs_devices->seed = cur_devices->seed;
  1327. cur_devices->seed = NULL;
  1328. lock_chunks(root);
  1329. __btrfs_close_devices(cur_devices);
  1330. unlock_chunks(root);
  1331. free_fs_devices(cur_devices);
  1332. }
  1333. root->fs_info->num_tolerated_disk_barrier_failures =
  1334. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1335. /*
  1336. * at this point, the device is zero sized. We want to
  1337. * remove it from the devices list and zero out the old super
  1338. */
  1339. if (clear_super && disk_super) {
  1340. /* make sure this device isn't detected as part of
  1341. * the FS anymore
  1342. */
  1343. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1344. set_buffer_dirty(bh);
  1345. sync_dirty_buffer(bh);
  1346. }
  1347. ret = 0;
  1348. /* Notify udev that device has changed */
  1349. btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
  1350. error_brelse:
  1351. brelse(bh);
  1352. if (bdev)
  1353. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1354. out:
  1355. mutex_unlock(&uuid_mutex);
  1356. return ret;
  1357. error_undo:
  1358. if (device->writeable) {
  1359. lock_chunks(root);
  1360. list_add(&device->dev_alloc_list,
  1361. &root->fs_info->fs_devices->alloc_list);
  1362. unlock_chunks(root);
  1363. root->fs_info->fs_devices->rw_devices++;
  1364. }
  1365. goto error_brelse;
  1366. }
  1367. void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
  1368. struct btrfs_device *srcdev)
  1369. {
  1370. WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
  1371. list_del_rcu(&srcdev->dev_list);
  1372. list_del_rcu(&srcdev->dev_alloc_list);
  1373. fs_info->fs_devices->num_devices--;
  1374. if (srcdev->missing) {
  1375. fs_info->fs_devices->missing_devices--;
  1376. fs_info->fs_devices->rw_devices++;
  1377. }
  1378. if (srcdev->can_discard)
  1379. fs_info->fs_devices->num_can_discard--;
  1380. if (srcdev->bdev)
  1381. fs_info->fs_devices->open_devices--;
  1382. call_rcu(&srcdev->rcu, free_device);
  1383. }
  1384. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  1385. struct btrfs_device *tgtdev)
  1386. {
  1387. struct btrfs_device *next_device;
  1388. WARN_ON(!tgtdev);
  1389. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1390. if (tgtdev->bdev) {
  1391. btrfs_scratch_superblock(tgtdev);
  1392. fs_info->fs_devices->open_devices--;
  1393. }
  1394. fs_info->fs_devices->num_devices--;
  1395. if (tgtdev->can_discard)
  1396. fs_info->fs_devices->num_can_discard++;
  1397. next_device = list_entry(fs_info->fs_devices->devices.next,
  1398. struct btrfs_device, dev_list);
  1399. if (tgtdev->bdev == fs_info->sb->s_bdev)
  1400. fs_info->sb->s_bdev = next_device->bdev;
  1401. if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
  1402. fs_info->fs_devices->latest_bdev = next_device->bdev;
  1403. list_del_rcu(&tgtdev->dev_list);
  1404. call_rcu(&tgtdev->rcu, free_device);
  1405. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1406. }
  1407. int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
  1408. struct btrfs_device **device)
  1409. {
  1410. int ret = 0;
  1411. struct btrfs_super_block *disk_super;
  1412. u64 devid;
  1413. u8 *dev_uuid;
  1414. struct block_device *bdev;
  1415. struct buffer_head *bh;
  1416. *device = NULL;
  1417. ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
  1418. root->fs_info->bdev_holder, 0, &bdev, &bh);
  1419. if (ret)
  1420. return ret;
  1421. disk_super = (struct btrfs_super_block *)bh->b_data;
  1422. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1423. dev_uuid = disk_super->dev_item.uuid;
  1424. *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1425. disk_super->fsid);
  1426. brelse(bh);
  1427. if (!*device)
  1428. ret = -ENOENT;
  1429. blkdev_put(bdev, FMODE_READ);
  1430. return ret;
  1431. }
  1432. int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
  1433. char *device_path,
  1434. struct btrfs_device **device)
  1435. {
  1436. *device = NULL;
  1437. if (strcmp(device_path, "missing") == 0) {
  1438. struct list_head *devices;
  1439. struct btrfs_device *tmp;
  1440. devices = &root->fs_info->fs_devices->devices;
  1441. /*
  1442. * It is safe to read the devices since the volume_mutex
  1443. * is held by the caller.
  1444. */
  1445. list_for_each_entry(tmp, devices, dev_list) {
  1446. if (tmp->in_fs_metadata && !tmp->bdev) {
  1447. *device = tmp;
  1448. break;
  1449. }
  1450. }
  1451. if (!*device) {
  1452. pr_err("btrfs: no missing device found\n");
  1453. return -ENOENT;
  1454. }
  1455. return 0;
  1456. } else {
  1457. return btrfs_find_device_by_path(root, device_path, device);
  1458. }
  1459. }
  1460. /*
  1461. * does all the dirty work required for changing file system's UUID.
  1462. */
  1463. static int btrfs_prepare_sprout(struct btrfs_root *root)
  1464. {
  1465. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1466. struct btrfs_fs_devices *old_devices;
  1467. struct btrfs_fs_devices *seed_devices;
  1468. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1469. struct btrfs_device *device;
  1470. u64 super_flags;
  1471. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1472. if (!fs_devices->seeding)
  1473. return -EINVAL;
  1474. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1475. if (!seed_devices)
  1476. return -ENOMEM;
  1477. old_devices = clone_fs_devices(fs_devices);
  1478. if (IS_ERR(old_devices)) {
  1479. kfree(seed_devices);
  1480. return PTR_ERR(old_devices);
  1481. }
  1482. list_add(&old_devices->list, &fs_uuids);
  1483. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1484. seed_devices->opened = 1;
  1485. INIT_LIST_HEAD(&seed_devices->devices);
  1486. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1487. mutex_init(&seed_devices->device_list_mutex);
  1488. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1489. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1490. synchronize_rcu);
  1491. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1492. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1493. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1494. device->fs_devices = seed_devices;
  1495. }
  1496. fs_devices->seeding = 0;
  1497. fs_devices->num_devices = 0;
  1498. fs_devices->open_devices = 0;
  1499. fs_devices->total_devices = 0;
  1500. fs_devices->seed = seed_devices;
  1501. generate_random_uuid(fs_devices->fsid);
  1502. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1503. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1504. super_flags = btrfs_super_flags(disk_super) &
  1505. ~BTRFS_SUPER_FLAG_SEEDING;
  1506. btrfs_set_super_flags(disk_super, super_flags);
  1507. return 0;
  1508. }
  1509. /*
  1510. * strore the expected generation for seed devices in device items.
  1511. */
  1512. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1513. struct btrfs_root *root)
  1514. {
  1515. struct btrfs_path *path;
  1516. struct extent_buffer *leaf;
  1517. struct btrfs_dev_item *dev_item;
  1518. struct btrfs_device *device;
  1519. struct btrfs_key key;
  1520. u8 fs_uuid[BTRFS_UUID_SIZE];
  1521. u8 dev_uuid[BTRFS_UUID_SIZE];
  1522. u64 devid;
  1523. int ret;
  1524. path = btrfs_alloc_path();
  1525. if (!path)
  1526. return -ENOMEM;
  1527. root = root->fs_info->chunk_root;
  1528. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1529. key.offset = 0;
  1530. key.type = BTRFS_DEV_ITEM_KEY;
  1531. while (1) {
  1532. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1533. if (ret < 0)
  1534. goto error;
  1535. leaf = path->nodes[0];
  1536. next_slot:
  1537. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1538. ret = btrfs_next_leaf(root, path);
  1539. if (ret > 0)
  1540. break;
  1541. if (ret < 0)
  1542. goto error;
  1543. leaf = path->nodes[0];
  1544. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1545. btrfs_release_path(path);
  1546. continue;
  1547. }
  1548. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1549. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1550. key.type != BTRFS_DEV_ITEM_KEY)
  1551. break;
  1552. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1553. struct btrfs_dev_item);
  1554. devid = btrfs_device_id(leaf, dev_item);
  1555. read_extent_buffer(leaf, dev_uuid,
  1556. (unsigned long)btrfs_device_uuid(dev_item),
  1557. BTRFS_UUID_SIZE);
  1558. read_extent_buffer(leaf, fs_uuid,
  1559. (unsigned long)btrfs_device_fsid(dev_item),
  1560. BTRFS_UUID_SIZE);
  1561. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1562. fs_uuid);
  1563. BUG_ON(!device); /* Logic error */
  1564. if (device->fs_devices->seeding) {
  1565. btrfs_set_device_generation(leaf, dev_item,
  1566. device->generation);
  1567. btrfs_mark_buffer_dirty(leaf);
  1568. }
  1569. path->slots[0]++;
  1570. goto next_slot;
  1571. }
  1572. ret = 0;
  1573. error:
  1574. btrfs_free_path(path);
  1575. return ret;
  1576. }
  1577. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1578. {
  1579. struct request_queue *q;
  1580. struct btrfs_trans_handle *trans;
  1581. struct btrfs_device *device;
  1582. struct block_device *bdev;
  1583. struct list_head *devices;
  1584. struct super_block *sb = root->fs_info->sb;
  1585. struct rcu_string *name;
  1586. u64 total_bytes;
  1587. int seeding_dev = 0;
  1588. int ret = 0;
  1589. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1590. return -EROFS;
  1591. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1592. root->fs_info->bdev_holder);
  1593. if (IS_ERR(bdev))
  1594. return PTR_ERR(bdev);
  1595. if (root->fs_info->fs_devices->seeding) {
  1596. seeding_dev = 1;
  1597. down_write(&sb->s_umount);
  1598. mutex_lock(&uuid_mutex);
  1599. }
  1600. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1601. devices = &root->fs_info->fs_devices->devices;
  1602. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1603. list_for_each_entry(device, devices, dev_list) {
  1604. if (device->bdev == bdev) {
  1605. ret = -EEXIST;
  1606. mutex_unlock(
  1607. &root->fs_info->fs_devices->device_list_mutex);
  1608. goto error;
  1609. }
  1610. }
  1611. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1612. device = kzalloc(sizeof(*device), GFP_NOFS);
  1613. if (!device) {
  1614. /* we can safely leave the fs_devices entry around */
  1615. ret = -ENOMEM;
  1616. goto error;
  1617. }
  1618. name = rcu_string_strdup(device_path, GFP_NOFS);
  1619. if (!name) {
  1620. kfree(device);
  1621. ret = -ENOMEM;
  1622. goto error;
  1623. }
  1624. rcu_assign_pointer(device->name, name);
  1625. ret = find_next_devid(root, &device->devid);
  1626. if (ret) {
  1627. rcu_string_free(device->name);
  1628. kfree(device);
  1629. goto error;
  1630. }
  1631. trans = btrfs_start_transaction(root, 0);
  1632. if (IS_ERR(trans)) {
  1633. rcu_string_free(device->name);
  1634. kfree(device);
  1635. ret = PTR_ERR(trans);
  1636. goto error;
  1637. }
  1638. lock_chunks(root);
  1639. q = bdev_get_queue(bdev);
  1640. if (blk_queue_discard(q))
  1641. device->can_discard = 1;
  1642. device->writeable = 1;
  1643. device->work.func = pending_bios_fn;
  1644. generate_random_uuid(device->uuid);
  1645. spin_lock_init(&device->io_lock);
  1646. device->generation = trans->transid;
  1647. device->io_width = root->sectorsize;
  1648. device->io_align = root->sectorsize;
  1649. device->sector_size = root->sectorsize;
  1650. device->total_bytes = i_size_read(bdev->bd_inode);
  1651. device->disk_total_bytes = device->total_bytes;
  1652. device->dev_root = root->fs_info->dev_root;
  1653. device->bdev = bdev;
  1654. device->in_fs_metadata = 1;
  1655. device->is_tgtdev_for_dev_replace = 0;
  1656. device->mode = FMODE_EXCL;
  1657. set_blocksize(device->bdev, 4096);
  1658. if (seeding_dev) {
  1659. sb->s_flags &= ~MS_RDONLY;
  1660. ret = btrfs_prepare_sprout(root);
  1661. BUG_ON(ret); /* -ENOMEM */
  1662. }
  1663. device->fs_devices = root->fs_info->fs_devices;
  1664. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1665. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1666. list_add(&device->dev_alloc_list,
  1667. &root->fs_info->fs_devices->alloc_list);
  1668. root->fs_info->fs_devices->num_devices++;
  1669. root->fs_info->fs_devices->open_devices++;
  1670. root->fs_info->fs_devices->rw_devices++;
  1671. root->fs_info->fs_devices->total_devices++;
  1672. if (device->can_discard)
  1673. root->fs_info->fs_devices->num_can_discard++;
  1674. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1675. spin_lock(&root->fs_info->free_chunk_lock);
  1676. root->fs_info->free_chunk_space += device->total_bytes;
  1677. spin_unlock(&root->fs_info->free_chunk_lock);
  1678. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1679. root->fs_info->fs_devices->rotating = 1;
  1680. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1681. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1682. total_bytes + device->total_bytes);
  1683. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1684. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1685. total_bytes + 1);
  1686. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1687. if (seeding_dev) {
  1688. ret = init_first_rw_device(trans, root, device);
  1689. if (ret) {
  1690. btrfs_abort_transaction(trans, root, ret);
  1691. goto error_trans;
  1692. }
  1693. ret = btrfs_finish_sprout(trans, root);
  1694. if (ret) {
  1695. btrfs_abort_transaction(trans, root, ret);
  1696. goto error_trans;
  1697. }
  1698. } else {
  1699. ret = btrfs_add_device(trans, root, device);
  1700. if (ret) {
  1701. btrfs_abort_transaction(trans, root, ret);
  1702. goto error_trans;
  1703. }
  1704. }
  1705. /*
  1706. * we've got more storage, clear any full flags on the space
  1707. * infos
  1708. */
  1709. btrfs_clear_space_info_full(root->fs_info);
  1710. unlock_chunks(root);
  1711. root->fs_info->num_tolerated_disk_barrier_failures =
  1712. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1713. ret = btrfs_commit_transaction(trans, root);
  1714. if (seeding_dev) {
  1715. mutex_unlock(&uuid_mutex);
  1716. up_write(&sb->s_umount);
  1717. if (ret) /* transaction commit */
  1718. return ret;
  1719. ret = btrfs_relocate_sys_chunks(root);
  1720. if (ret < 0)
  1721. btrfs_error(root->fs_info, ret,
  1722. "Failed to relocate sys chunks after "
  1723. "device initialization. This can be fixed "
  1724. "using the \"btrfs balance\" command.");
  1725. trans = btrfs_attach_transaction(root);
  1726. if (IS_ERR(trans)) {
  1727. if (PTR_ERR(trans) == -ENOENT)
  1728. return 0;
  1729. return PTR_ERR(trans);
  1730. }
  1731. ret = btrfs_commit_transaction(trans, root);
  1732. }
  1733. return ret;
  1734. error_trans:
  1735. unlock_chunks(root);
  1736. btrfs_end_transaction(trans, root);
  1737. rcu_string_free(device->name);
  1738. kfree(device);
  1739. error:
  1740. blkdev_put(bdev, FMODE_EXCL);
  1741. if (seeding_dev) {
  1742. mutex_unlock(&uuid_mutex);
  1743. up_write(&sb->s_umount);
  1744. }
  1745. return ret;
  1746. }
  1747. int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
  1748. struct btrfs_device **device_out)
  1749. {
  1750. struct request_queue *q;
  1751. struct btrfs_device *device;
  1752. struct block_device *bdev;
  1753. struct btrfs_fs_info *fs_info = root->fs_info;
  1754. struct list_head *devices;
  1755. struct rcu_string *name;
  1756. int ret = 0;
  1757. *device_out = NULL;
  1758. if (fs_info->fs_devices->seeding)
  1759. return -EINVAL;
  1760. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1761. fs_info->bdev_holder);
  1762. if (IS_ERR(bdev))
  1763. return PTR_ERR(bdev);
  1764. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1765. devices = &fs_info->fs_devices->devices;
  1766. list_for_each_entry(device, devices, dev_list) {
  1767. if (device->bdev == bdev) {
  1768. ret = -EEXIST;
  1769. goto error;
  1770. }
  1771. }
  1772. device = kzalloc(sizeof(*device), GFP_NOFS);
  1773. if (!device) {
  1774. ret = -ENOMEM;
  1775. goto error;
  1776. }
  1777. name = rcu_string_strdup(device_path, GFP_NOFS);
  1778. if (!name) {
  1779. kfree(device);
  1780. ret = -ENOMEM;
  1781. goto error;
  1782. }
  1783. rcu_assign_pointer(device->name, name);
  1784. q = bdev_get_queue(bdev);
  1785. if (blk_queue_discard(q))
  1786. device->can_discard = 1;
  1787. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1788. device->writeable = 1;
  1789. device->work.func = pending_bios_fn;
  1790. generate_random_uuid(device->uuid);
  1791. device->devid = BTRFS_DEV_REPLACE_DEVID;
  1792. spin_lock_init(&device->io_lock);
  1793. device->generation = 0;
  1794. device->io_width = root->sectorsize;
  1795. device->io_align = root->sectorsize;
  1796. device->sector_size = root->sectorsize;
  1797. device->total_bytes = i_size_read(bdev->bd_inode);
  1798. device->disk_total_bytes = device->total_bytes;
  1799. device->dev_root = fs_info->dev_root;
  1800. device->bdev = bdev;
  1801. device->in_fs_metadata = 1;
  1802. device->is_tgtdev_for_dev_replace = 1;
  1803. device->mode = FMODE_EXCL;
  1804. set_blocksize(device->bdev, 4096);
  1805. device->fs_devices = fs_info->fs_devices;
  1806. list_add(&device->dev_list, &fs_info->fs_devices->devices);
  1807. fs_info->fs_devices->num_devices++;
  1808. fs_info->fs_devices->open_devices++;
  1809. if (device->can_discard)
  1810. fs_info->fs_devices->num_can_discard++;
  1811. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1812. *device_out = device;
  1813. return ret;
  1814. error:
  1815. blkdev_put(bdev, FMODE_EXCL);
  1816. return ret;
  1817. }
  1818. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  1819. struct btrfs_device *tgtdev)
  1820. {
  1821. WARN_ON(fs_info->fs_devices->rw_devices == 0);
  1822. tgtdev->io_width = fs_info->dev_root->sectorsize;
  1823. tgtdev->io_align = fs_info->dev_root->sectorsize;
  1824. tgtdev->sector_size = fs_info->dev_root->sectorsize;
  1825. tgtdev->dev_root = fs_info->dev_root;
  1826. tgtdev->in_fs_metadata = 1;
  1827. }
  1828. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1829. struct btrfs_device *device)
  1830. {
  1831. int ret;
  1832. struct btrfs_path *path;
  1833. struct btrfs_root *root;
  1834. struct btrfs_dev_item *dev_item;
  1835. struct extent_buffer *leaf;
  1836. struct btrfs_key key;
  1837. root = device->dev_root->fs_info->chunk_root;
  1838. path = btrfs_alloc_path();
  1839. if (!path)
  1840. return -ENOMEM;
  1841. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1842. key.type = BTRFS_DEV_ITEM_KEY;
  1843. key.offset = device->devid;
  1844. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1845. if (ret < 0)
  1846. goto out;
  1847. if (ret > 0) {
  1848. ret = -ENOENT;
  1849. goto out;
  1850. }
  1851. leaf = path->nodes[0];
  1852. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1853. btrfs_set_device_id(leaf, dev_item, device->devid);
  1854. btrfs_set_device_type(leaf, dev_item, device->type);
  1855. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1856. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1857. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1858. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1859. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1860. btrfs_mark_buffer_dirty(leaf);
  1861. out:
  1862. btrfs_free_path(path);
  1863. return ret;
  1864. }
  1865. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1866. struct btrfs_device *device, u64 new_size)
  1867. {
  1868. struct btrfs_super_block *super_copy =
  1869. device->dev_root->fs_info->super_copy;
  1870. u64 old_total = btrfs_super_total_bytes(super_copy);
  1871. u64 diff = new_size - device->total_bytes;
  1872. if (!device->writeable)
  1873. return -EACCES;
  1874. if (new_size <= device->total_bytes ||
  1875. device->is_tgtdev_for_dev_replace)
  1876. return -EINVAL;
  1877. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1878. device->fs_devices->total_rw_bytes += diff;
  1879. device->total_bytes = new_size;
  1880. device->disk_total_bytes = new_size;
  1881. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1882. return btrfs_update_device(trans, device);
  1883. }
  1884. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1885. struct btrfs_device *device, u64 new_size)
  1886. {
  1887. int ret;
  1888. lock_chunks(device->dev_root);
  1889. ret = __btrfs_grow_device(trans, device, new_size);
  1890. unlock_chunks(device->dev_root);
  1891. return ret;
  1892. }
  1893. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1894. struct btrfs_root *root,
  1895. u64 chunk_tree, u64 chunk_objectid,
  1896. u64 chunk_offset)
  1897. {
  1898. int ret;
  1899. struct btrfs_path *path;
  1900. struct btrfs_key key;
  1901. root = root->fs_info->chunk_root;
  1902. path = btrfs_alloc_path();
  1903. if (!path)
  1904. return -ENOMEM;
  1905. key.objectid = chunk_objectid;
  1906. key.offset = chunk_offset;
  1907. key.type = BTRFS_CHUNK_ITEM_KEY;
  1908. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1909. if (ret < 0)
  1910. goto out;
  1911. else if (ret > 0) { /* Logic error or corruption */
  1912. btrfs_error(root->fs_info, -ENOENT,
  1913. "Failed lookup while freeing chunk.");
  1914. ret = -ENOENT;
  1915. goto out;
  1916. }
  1917. ret = btrfs_del_item(trans, root, path);
  1918. if (ret < 0)
  1919. btrfs_error(root->fs_info, ret,
  1920. "Failed to delete chunk item.");
  1921. out:
  1922. btrfs_free_path(path);
  1923. return ret;
  1924. }
  1925. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1926. chunk_offset)
  1927. {
  1928. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1929. struct btrfs_disk_key *disk_key;
  1930. struct btrfs_chunk *chunk;
  1931. u8 *ptr;
  1932. int ret = 0;
  1933. u32 num_stripes;
  1934. u32 array_size;
  1935. u32 len = 0;
  1936. u32 cur;
  1937. struct btrfs_key key;
  1938. array_size = btrfs_super_sys_array_size(super_copy);
  1939. ptr = super_copy->sys_chunk_array;
  1940. cur = 0;
  1941. while (cur < array_size) {
  1942. disk_key = (struct btrfs_disk_key *)ptr;
  1943. btrfs_disk_key_to_cpu(&key, disk_key);
  1944. len = sizeof(*disk_key);
  1945. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1946. chunk = (struct btrfs_chunk *)(ptr + len);
  1947. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1948. len += btrfs_chunk_item_size(num_stripes);
  1949. } else {
  1950. ret = -EIO;
  1951. break;
  1952. }
  1953. if (key.objectid == chunk_objectid &&
  1954. key.offset == chunk_offset) {
  1955. memmove(ptr, ptr + len, array_size - (cur + len));
  1956. array_size -= len;
  1957. btrfs_set_super_sys_array_size(super_copy, array_size);
  1958. } else {
  1959. ptr += len;
  1960. cur += len;
  1961. }
  1962. }
  1963. return ret;
  1964. }
  1965. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1966. u64 chunk_tree, u64 chunk_objectid,
  1967. u64 chunk_offset)
  1968. {
  1969. struct extent_map_tree *em_tree;
  1970. struct btrfs_root *extent_root;
  1971. struct btrfs_trans_handle *trans;
  1972. struct extent_map *em;
  1973. struct map_lookup *map;
  1974. int ret;
  1975. int i;
  1976. root = root->fs_info->chunk_root;
  1977. extent_root = root->fs_info->extent_root;
  1978. em_tree = &root->fs_info->mapping_tree.map_tree;
  1979. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1980. if (ret)
  1981. return -ENOSPC;
  1982. /* step one, relocate all the extents inside this chunk */
  1983. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1984. if (ret)
  1985. return ret;
  1986. trans = btrfs_start_transaction(root, 0);
  1987. BUG_ON(IS_ERR(trans));
  1988. lock_chunks(root);
  1989. /*
  1990. * step two, delete the device extents and the
  1991. * chunk tree entries
  1992. */
  1993. read_lock(&em_tree->lock);
  1994. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1995. read_unlock(&em_tree->lock);
  1996. BUG_ON(!em || em->start > chunk_offset ||
  1997. em->start + em->len < chunk_offset);
  1998. map = (struct map_lookup *)em->bdev;
  1999. for (i = 0; i < map->num_stripes; i++) {
  2000. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  2001. map->stripes[i].physical);
  2002. BUG_ON(ret);
  2003. if (map->stripes[i].dev) {
  2004. ret = btrfs_update_device(trans, map->stripes[i].dev);
  2005. BUG_ON(ret);
  2006. }
  2007. }
  2008. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  2009. chunk_offset);
  2010. BUG_ON(ret);
  2011. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  2012. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2013. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  2014. BUG_ON(ret);
  2015. }
  2016. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  2017. BUG_ON(ret);
  2018. write_lock(&em_tree->lock);
  2019. remove_extent_mapping(em_tree, em);
  2020. write_unlock(&em_tree->lock);
  2021. kfree(map);
  2022. em->bdev = NULL;
  2023. /* once for the tree */
  2024. free_extent_map(em);
  2025. /* once for us */
  2026. free_extent_map(em);
  2027. unlock_chunks(root);
  2028. btrfs_end_transaction(trans, root);
  2029. return 0;
  2030. }
  2031. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  2032. {
  2033. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  2034. struct btrfs_path *path;
  2035. struct extent_buffer *leaf;
  2036. struct btrfs_chunk *chunk;
  2037. struct btrfs_key key;
  2038. struct btrfs_key found_key;
  2039. u64 chunk_tree = chunk_root->root_key.objectid;
  2040. u64 chunk_type;
  2041. bool retried = false;
  2042. int failed = 0;
  2043. int ret;
  2044. path = btrfs_alloc_path();
  2045. if (!path)
  2046. return -ENOMEM;
  2047. again:
  2048. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2049. key.offset = (u64)-1;
  2050. key.type = BTRFS_CHUNK_ITEM_KEY;
  2051. while (1) {
  2052. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2053. if (ret < 0)
  2054. goto error;
  2055. BUG_ON(ret == 0); /* Corruption */
  2056. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  2057. key.type);
  2058. if (ret < 0)
  2059. goto error;
  2060. if (ret > 0)
  2061. break;
  2062. leaf = path->nodes[0];
  2063. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2064. chunk = btrfs_item_ptr(leaf, path->slots[0],
  2065. struct btrfs_chunk);
  2066. chunk_type = btrfs_chunk_type(leaf, chunk);
  2067. btrfs_release_path(path);
  2068. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2069. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  2070. found_key.objectid,
  2071. found_key.offset);
  2072. if (ret == -ENOSPC)
  2073. failed++;
  2074. else if (ret)
  2075. BUG();
  2076. }
  2077. if (found_key.offset == 0)
  2078. break;
  2079. key.offset = found_key.offset - 1;
  2080. }
  2081. ret = 0;
  2082. if (failed && !retried) {
  2083. failed = 0;
  2084. retried = true;
  2085. goto again;
  2086. } else if (failed && retried) {
  2087. WARN_ON(1);
  2088. ret = -ENOSPC;
  2089. }
  2090. error:
  2091. btrfs_free_path(path);
  2092. return ret;
  2093. }
  2094. static int insert_balance_item(struct btrfs_root *root,
  2095. struct btrfs_balance_control *bctl)
  2096. {
  2097. struct btrfs_trans_handle *trans;
  2098. struct btrfs_balance_item *item;
  2099. struct btrfs_disk_balance_args disk_bargs;
  2100. struct btrfs_path *path;
  2101. struct extent_buffer *leaf;
  2102. struct btrfs_key key;
  2103. int ret, err;
  2104. path = btrfs_alloc_path();
  2105. if (!path)
  2106. return -ENOMEM;
  2107. trans = btrfs_start_transaction(root, 0);
  2108. if (IS_ERR(trans)) {
  2109. btrfs_free_path(path);
  2110. return PTR_ERR(trans);
  2111. }
  2112. key.objectid = BTRFS_BALANCE_OBJECTID;
  2113. key.type = BTRFS_BALANCE_ITEM_KEY;
  2114. key.offset = 0;
  2115. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2116. sizeof(*item));
  2117. if (ret)
  2118. goto out;
  2119. leaf = path->nodes[0];
  2120. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2121. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  2122. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  2123. btrfs_set_balance_data(leaf, item, &disk_bargs);
  2124. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  2125. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  2126. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  2127. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  2128. btrfs_set_balance_flags(leaf, item, bctl->flags);
  2129. btrfs_mark_buffer_dirty(leaf);
  2130. out:
  2131. btrfs_free_path(path);
  2132. err = btrfs_commit_transaction(trans, root);
  2133. if (err && !ret)
  2134. ret = err;
  2135. return ret;
  2136. }
  2137. static int del_balance_item(struct btrfs_root *root)
  2138. {
  2139. struct btrfs_trans_handle *trans;
  2140. struct btrfs_path *path;
  2141. struct btrfs_key key;
  2142. int ret, err;
  2143. path = btrfs_alloc_path();
  2144. if (!path)
  2145. return -ENOMEM;
  2146. trans = btrfs_start_transaction(root, 0);
  2147. if (IS_ERR(trans)) {
  2148. btrfs_free_path(path);
  2149. return PTR_ERR(trans);
  2150. }
  2151. key.objectid = BTRFS_BALANCE_OBJECTID;
  2152. key.type = BTRFS_BALANCE_ITEM_KEY;
  2153. key.offset = 0;
  2154. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2155. if (ret < 0)
  2156. goto out;
  2157. if (ret > 0) {
  2158. ret = -ENOENT;
  2159. goto out;
  2160. }
  2161. ret = btrfs_del_item(trans, root, path);
  2162. out:
  2163. btrfs_free_path(path);
  2164. err = btrfs_commit_transaction(trans, root);
  2165. if (err && !ret)
  2166. ret = err;
  2167. return ret;
  2168. }
  2169. /*
  2170. * This is a heuristic used to reduce the number of chunks balanced on
  2171. * resume after balance was interrupted.
  2172. */
  2173. static void update_balance_args(struct btrfs_balance_control *bctl)
  2174. {
  2175. /*
  2176. * Turn on soft mode for chunk types that were being converted.
  2177. */
  2178. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2179. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2180. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2181. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2182. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2183. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2184. /*
  2185. * Turn on usage filter if is not already used. The idea is
  2186. * that chunks that we have already balanced should be
  2187. * reasonably full. Don't do it for chunks that are being
  2188. * converted - that will keep us from relocating unconverted
  2189. * (albeit full) chunks.
  2190. */
  2191. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2192. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2193. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2194. bctl->data.usage = 90;
  2195. }
  2196. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2197. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2198. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2199. bctl->sys.usage = 90;
  2200. }
  2201. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2202. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2203. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2204. bctl->meta.usage = 90;
  2205. }
  2206. }
  2207. /*
  2208. * Should be called with both balance and volume mutexes held to
  2209. * serialize other volume operations (add_dev/rm_dev/resize) with
  2210. * restriper. Same goes for unset_balance_control.
  2211. */
  2212. static void set_balance_control(struct btrfs_balance_control *bctl)
  2213. {
  2214. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2215. BUG_ON(fs_info->balance_ctl);
  2216. spin_lock(&fs_info->balance_lock);
  2217. fs_info->balance_ctl = bctl;
  2218. spin_unlock(&fs_info->balance_lock);
  2219. }
  2220. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  2221. {
  2222. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2223. BUG_ON(!fs_info->balance_ctl);
  2224. spin_lock(&fs_info->balance_lock);
  2225. fs_info->balance_ctl = NULL;
  2226. spin_unlock(&fs_info->balance_lock);
  2227. kfree(bctl);
  2228. }
  2229. /*
  2230. * Balance filters. Return 1 if chunk should be filtered out
  2231. * (should not be balanced).
  2232. */
  2233. static int chunk_profiles_filter(u64 chunk_type,
  2234. struct btrfs_balance_args *bargs)
  2235. {
  2236. chunk_type = chunk_to_extended(chunk_type) &
  2237. BTRFS_EXTENDED_PROFILE_MASK;
  2238. if (bargs->profiles & chunk_type)
  2239. return 0;
  2240. return 1;
  2241. }
  2242. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2243. struct btrfs_balance_args *bargs)
  2244. {
  2245. struct btrfs_block_group_cache *cache;
  2246. u64 chunk_used, user_thresh;
  2247. int ret = 1;
  2248. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2249. chunk_used = btrfs_block_group_used(&cache->item);
  2250. if (bargs->usage == 0)
  2251. user_thresh = 0;
  2252. else if (bargs->usage > 100)
  2253. user_thresh = cache->key.offset;
  2254. else
  2255. user_thresh = div_factor_fine(cache->key.offset,
  2256. bargs->usage);
  2257. if (chunk_used < user_thresh)
  2258. ret = 0;
  2259. btrfs_put_block_group(cache);
  2260. return ret;
  2261. }
  2262. static int chunk_devid_filter(struct extent_buffer *leaf,
  2263. struct btrfs_chunk *chunk,
  2264. struct btrfs_balance_args *bargs)
  2265. {
  2266. struct btrfs_stripe *stripe;
  2267. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2268. int i;
  2269. for (i = 0; i < num_stripes; i++) {
  2270. stripe = btrfs_stripe_nr(chunk, i);
  2271. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2272. return 0;
  2273. }
  2274. return 1;
  2275. }
  2276. /* [pstart, pend) */
  2277. static int chunk_drange_filter(struct extent_buffer *leaf,
  2278. struct btrfs_chunk *chunk,
  2279. u64 chunk_offset,
  2280. struct btrfs_balance_args *bargs)
  2281. {
  2282. struct btrfs_stripe *stripe;
  2283. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2284. u64 stripe_offset;
  2285. u64 stripe_length;
  2286. int factor;
  2287. int i;
  2288. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2289. return 0;
  2290. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2291. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
  2292. factor = 2;
  2293. else
  2294. factor = 1;
  2295. factor = num_stripes / factor;
  2296. for (i = 0; i < num_stripes; i++) {
  2297. stripe = btrfs_stripe_nr(chunk, i);
  2298. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2299. continue;
  2300. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2301. stripe_length = btrfs_chunk_length(leaf, chunk);
  2302. do_div(stripe_length, factor);
  2303. if (stripe_offset < bargs->pend &&
  2304. stripe_offset + stripe_length > bargs->pstart)
  2305. return 0;
  2306. }
  2307. return 1;
  2308. }
  2309. /* [vstart, vend) */
  2310. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2311. struct btrfs_chunk *chunk,
  2312. u64 chunk_offset,
  2313. struct btrfs_balance_args *bargs)
  2314. {
  2315. if (chunk_offset < bargs->vend &&
  2316. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2317. /* at least part of the chunk is inside this vrange */
  2318. return 0;
  2319. return 1;
  2320. }
  2321. static int chunk_soft_convert_filter(u64 chunk_type,
  2322. struct btrfs_balance_args *bargs)
  2323. {
  2324. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2325. return 0;
  2326. chunk_type = chunk_to_extended(chunk_type) &
  2327. BTRFS_EXTENDED_PROFILE_MASK;
  2328. if (bargs->target == chunk_type)
  2329. return 1;
  2330. return 0;
  2331. }
  2332. static int should_balance_chunk(struct btrfs_root *root,
  2333. struct extent_buffer *leaf,
  2334. struct btrfs_chunk *chunk, u64 chunk_offset)
  2335. {
  2336. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2337. struct btrfs_balance_args *bargs = NULL;
  2338. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2339. /* type filter */
  2340. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2341. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2342. return 0;
  2343. }
  2344. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2345. bargs = &bctl->data;
  2346. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2347. bargs = &bctl->sys;
  2348. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2349. bargs = &bctl->meta;
  2350. /* profiles filter */
  2351. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2352. chunk_profiles_filter(chunk_type, bargs)) {
  2353. return 0;
  2354. }
  2355. /* usage filter */
  2356. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2357. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  2358. return 0;
  2359. }
  2360. /* devid filter */
  2361. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2362. chunk_devid_filter(leaf, chunk, bargs)) {
  2363. return 0;
  2364. }
  2365. /* drange filter, makes sense only with devid filter */
  2366. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2367. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  2368. return 0;
  2369. }
  2370. /* vrange filter */
  2371. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2372. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2373. return 0;
  2374. }
  2375. /* soft profile changing mode */
  2376. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2377. chunk_soft_convert_filter(chunk_type, bargs)) {
  2378. return 0;
  2379. }
  2380. return 1;
  2381. }
  2382. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2383. {
  2384. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2385. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2386. struct btrfs_root *dev_root = fs_info->dev_root;
  2387. struct list_head *devices;
  2388. struct btrfs_device *device;
  2389. u64 old_size;
  2390. u64 size_to_free;
  2391. struct btrfs_chunk *chunk;
  2392. struct btrfs_path *path;
  2393. struct btrfs_key key;
  2394. struct btrfs_key found_key;
  2395. struct btrfs_trans_handle *trans;
  2396. struct extent_buffer *leaf;
  2397. int slot;
  2398. int ret;
  2399. int enospc_errors = 0;
  2400. bool counting = true;
  2401. /* step one make some room on all the devices */
  2402. devices = &fs_info->fs_devices->devices;
  2403. list_for_each_entry(device, devices, dev_list) {
  2404. old_size = device->total_bytes;
  2405. size_to_free = div_factor(old_size, 1);
  2406. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2407. if (!device->writeable ||
  2408. device->total_bytes - device->bytes_used > size_to_free ||
  2409. device->is_tgtdev_for_dev_replace)
  2410. continue;
  2411. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2412. if (ret == -ENOSPC)
  2413. break;
  2414. BUG_ON(ret);
  2415. trans = btrfs_start_transaction(dev_root, 0);
  2416. BUG_ON(IS_ERR(trans));
  2417. ret = btrfs_grow_device(trans, device, old_size);
  2418. BUG_ON(ret);
  2419. btrfs_end_transaction(trans, dev_root);
  2420. }
  2421. /* step two, relocate all the chunks */
  2422. path = btrfs_alloc_path();
  2423. if (!path) {
  2424. ret = -ENOMEM;
  2425. goto error;
  2426. }
  2427. /* zero out stat counters */
  2428. spin_lock(&fs_info->balance_lock);
  2429. memset(&bctl->stat, 0, sizeof(bctl->stat));
  2430. spin_unlock(&fs_info->balance_lock);
  2431. again:
  2432. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2433. key.offset = (u64)-1;
  2434. key.type = BTRFS_CHUNK_ITEM_KEY;
  2435. while (1) {
  2436. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  2437. atomic_read(&fs_info->balance_cancel_req)) {
  2438. ret = -ECANCELED;
  2439. goto error;
  2440. }
  2441. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2442. if (ret < 0)
  2443. goto error;
  2444. /*
  2445. * this shouldn't happen, it means the last relocate
  2446. * failed
  2447. */
  2448. if (ret == 0)
  2449. BUG(); /* FIXME break ? */
  2450. ret = btrfs_previous_item(chunk_root, path, 0,
  2451. BTRFS_CHUNK_ITEM_KEY);
  2452. if (ret) {
  2453. ret = 0;
  2454. break;
  2455. }
  2456. leaf = path->nodes[0];
  2457. slot = path->slots[0];
  2458. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2459. if (found_key.objectid != key.objectid)
  2460. break;
  2461. /* chunk zero is special */
  2462. if (found_key.offset == 0)
  2463. break;
  2464. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2465. if (!counting) {
  2466. spin_lock(&fs_info->balance_lock);
  2467. bctl->stat.considered++;
  2468. spin_unlock(&fs_info->balance_lock);
  2469. }
  2470. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2471. found_key.offset);
  2472. btrfs_release_path(path);
  2473. if (!ret)
  2474. goto loop;
  2475. if (counting) {
  2476. spin_lock(&fs_info->balance_lock);
  2477. bctl->stat.expected++;
  2478. spin_unlock(&fs_info->balance_lock);
  2479. goto loop;
  2480. }
  2481. ret = btrfs_relocate_chunk(chunk_root,
  2482. chunk_root->root_key.objectid,
  2483. found_key.objectid,
  2484. found_key.offset);
  2485. if (ret && ret != -ENOSPC)
  2486. goto error;
  2487. if (ret == -ENOSPC) {
  2488. enospc_errors++;
  2489. } else {
  2490. spin_lock(&fs_info->balance_lock);
  2491. bctl->stat.completed++;
  2492. spin_unlock(&fs_info->balance_lock);
  2493. }
  2494. loop:
  2495. key.offset = found_key.offset - 1;
  2496. }
  2497. if (counting) {
  2498. btrfs_release_path(path);
  2499. counting = false;
  2500. goto again;
  2501. }
  2502. error:
  2503. btrfs_free_path(path);
  2504. if (enospc_errors) {
  2505. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2506. enospc_errors);
  2507. if (!ret)
  2508. ret = -ENOSPC;
  2509. }
  2510. return ret;
  2511. }
  2512. /**
  2513. * alloc_profile_is_valid - see if a given profile is valid and reduced
  2514. * @flags: profile to validate
  2515. * @extended: if true @flags is treated as an extended profile
  2516. */
  2517. static int alloc_profile_is_valid(u64 flags, int extended)
  2518. {
  2519. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  2520. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  2521. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  2522. /* 1) check that all other bits are zeroed */
  2523. if (flags & ~mask)
  2524. return 0;
  2525. /* 2) see if profile is reduced */
  2526. if (flags == 0)
  2527. return !extended; /* "0" is valid for usual profiles */
  2528. /* true if exactly one bit set */
  2529. return (flags & (flags - 1)) == 0;
  2530. }
  2531. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  2532. {
  2533. /* cancel requested || normal exit path */
  2534. return atomic_read(&fs_info->balance_cancel_req) ||
  2535. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  2536. atomic_read(&fs_info->balance_cancel_req) == 0);
  2537. }
  2538. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2539. {
  2540. int ret;
  2541. unset_balance_control(fs_info);
  2542. ret = del_balance_item(fs_info->tree_root);
  2543. BUG_ON(ret);
  2544. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2545. }
  2546. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2547. struct btrfs_ioctl_balance_args *bargs);
  2548. /*
  2549. * Should be called with both balance and volume mutexes held
  2550. */
  2551. int btrfs_balance(struct btrfs_balance_control *bctl,
  2552. struct btrfs_ioctl_balance_args *bargs)
  2553. {
  2554. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2555. u64 allowed;
  2556. int mixed = 0;
  2557. int ret;
  2558. u64 num_devices;
  2559. if (btrfs_fs_closing(fs_info) ||
  2560. atomic_read(&fs_info->balance_pause_req) ||
  2561. atomic_read(&fs_info->balance_cancel_req)) {
  2562. ret = -EINVAL;
  2563. goto out;
  2564. }
  2565. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2566. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  2567. mixed = 1;
  2568. /*
  2569. * In case of mixed groups both data and meta should be picked,
  2570. * and identical options should be given for both of them.
  2571. */
  2572. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  2573. if (mixed && (bctl->flags & allowed)) {
  2574. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2575. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2576. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2577. printk(KERN_ERR "btrfs: with mixed groups data and "
  2578. "metadata balance options must be the same\n");
  2579. ret = -EINVAL;
  2580. goto out;
  2581. }
  2582. }
  2583. num_devices = fs_info->fs_devices->num_devices;
  2584. btrfs_dev_replace_lock(&fs_info->dev_replace);
  2585. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  2586. BUG_ON(num_devices < 1);
  2587. num_devices--;
  2588. }
  2589. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  2590. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2591. if (num_devices == 1)
  2592. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2593. else if (num_devices < 4)
  2594. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2595. else
  2596. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2597. BTRFS_BLOCK_GROUP_RAID10);
  2598. if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2599. (!alloc_profile_is_valid(bctl->data.target, 1) ||
  2600. (bctl->data.target & ~allowed))) {
  2601. printk(KERN_ERR "btrfs: unable to start balance with target "
  2602. "data profile %llu\n",
  2603. (unsigned long long)bctl->data.target);
  2604. ret = -EINVAL;
  2605. goto out;
  2606. }
  2607. if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2608. (!alloc_profile_is_valid(bctl->meta.target, 1) ||
  2609. (bctl->meta.target & ~allowed))) {
  2610. printk(KERN_ERR "btrfs: unable to start balance with target "
  2611. "metadata profile %llu\n",
  2612. (unsigned long long)bctl->meta.target);
  2613. ret = -EINVAL;
  2614. goto out;
  2615. }
  2616. if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2617. (!alloc_profile_is_valid(bctl->sys.target, 1) ||
  2618. (bctl->sys.target & ~allowed))) {
  2619. printk(KERN_ERR "btrfs: unable to start balance with target "
  2620. "system profile %llu\n",
  2621. (unsigned long long)bctl->sys.target);
  2622. ret = -EINVAL;
  2623. goto out;
  2624. }
  2625. /* allow dup'ed data chunks only in mixed mode */
  2626. if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2627. (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
  2628. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2629. ret = -EINVAL;
  2630. goto out;
  2631. }
  2632. /* allow to reduce meta or sys integrity only if force set */
  2633. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2634. BTRFS_BLOCK_GROUP_RAID10;
  2635. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2636. (fs_info->avail_system_alloc_bits & allowed) &&
  2637. !(bctl->sys.target & allowed)) ||
  2638. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2639. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2640. !(bctl->meta.target & allowed))) {
  2641. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2642. printk(KERN_INFO "btrfs: force reducing metadata "
  2643. "integrity\n");
  2644. } else {
  2645. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2646. "integrity, use force if you want this\n");
  2647. ret = -EINVAL;
  2648. goto out;
  2649. }
  2650. }
  2651. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2652. int num_tolerated_disk_barrier_failures;
  2653. u64 target = bctl->sys.target;
  2654. num_tolerated_disk_barrier_failures =
  2655. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2656. if (num_tolerated_disk_barrier_failures > 0 &&
  2657. (target &
  2658. (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  2659. BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
  2660. num_tolerated_disk_barrier_failures = 0;
  2661. else if (num_tolerated_disk_barrier_failures > 1 &&
  2662. (target &
  2663. (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
  2664. num_tolerated_disk_barrier_failures = 1;
  2665. fs_info->num_tolerated_disk_barrier_failures =
  2666. num_tolerated_disk_barrier_failures;
  2667. }
  2668. ret = insert_balance_item(fs_info->tree_root, bctl);
  2669. if (ret && ret != -EEXIST)
  2670. goto out;
  2671. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2672. BUG_ON(ret == -EEXIST);
  2673. set_balance_control(bctl);
  2674. } else {
  2675. BUG_ON(ret != -EEXIST);
  2676. spin_lock(&fs_info->balance_lock);
  2677. update_balance_args(bctl);
  2678. spin_unlock(&fs_info->balance_lock);
  2679. }
  2680. atomic_inc(&fs_info->balance_running);
  2681. mutex_unlock(&fs_info->balance_mutex);
  2682. ret = __btrfs_balance(fs_info);
  2683. mutex_lock(&fs_info->balance_mutex);
  2684. atomic_dec(&fs_info->balance_running);
  2685. if (bargs) {
  2686. memset(bargs, 0, sizeof(*bargs));
  2687. update_ioctl_balance_args(fs_info, 0, bargs);
  2688. }
  2689. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  2690. balance_need_close(fs_info)) {
  2691. __cancel_balance(fs_info);
  2692. }
  2693. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2694. fs_info->num_tolerated_disk_barrier_failures =
  2695. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2696. }
  2697. wake_up(&fs_info->balance_wait_q);
  2698. return ret;
  2699. out:
  2700. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2701. __cancel_balance(fs_info);
  2702. else {
  2703. kfree(bctl);
  2704. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2705. }
  2706. return ret;
  2707. }
  2708. static int balance_kthread(void *data)
  2709. {
  2710. struct btrfs_fs_info *fs_info = data;
  2711. int ret = 0;
  2712. mutex_lock(&fs_info->volume_mutex);
  2713. mutex_lock(&fs_info->balance_mutex);
  2714. if (fs_info->balance_ctl) {
  2715. printk(KERN_INFO "btrfs: continuing balance\n");
  2716. ret = btrfs_balance(fs_info->balance_ctl, NULL);
  2717. }
  2718. mutex_unlock(&fs_info->balance_mutex);
  2719. mutex_unlock(&fs_info->volume_mutex);
  2720. return ret;
  2721. }
  2722. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  2723. {
  2724. struct task_struct *tsk;
  2725. spin_lock(&fs_info->balance_lock);
  2726. if (!fs_info->balance_ctl) {
  2727. spin_unlock(&fs_info->balance_lock);
  2728. return 0;
  2729. }
  2730. spin_unlock(&fs_info->balance_lock);
  2731. if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
  2732. printk(KERN_INFO "btrfs: force skipping balance\n");
  2733. return 0;
  2734. }
  2735. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  2736. if (IS_ERR(tsk))
  2737. return PTR_ERR(tsk);
  2738. return 0;
  2739. }
  2740. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  2741. {
  2742. struct btrfs_balance_control *bctl;
  2743. struct btrfs_balance_item *item;
  2744. struct btrfs_disk_balance_args disk_bargs;
  2745. struct btrfs_path *path;
  2746. struct extent_buffer *leaf;
  2747. struct btrfs_key key;
  2748. int ret;
  2749. path = btrfs_alloc_path();
  2750. if (!path)
  2751. return -ENOMEM;
  2752. key.objectid = BTRFS_BALANCE_OBJECTID;
  2753. key.type = BTRFS_BALANCE_ITEM_KEY;
  2754. key.offset = 0;
  2755. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  2756. if (ret < 0)
  2757. goto out;
  2758. if (ret > 0) { /* ret = -ENOENT; */
  2759. ret = 0;
  2760. goto out;
  2761. }
  2762. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2763. if (!bctl) {
  2764. ret = -ENOMEM;
  2765. goto out;
  2766. }
  2767. leaf = path->nodes[0];
  2768. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2769. bctl->fs_info = fs_info;
  2770. bctl->flags = btrfs_balance_flags(leaf, item);
  2771. bctl->flags |= BTRFS_BALANCE_RESUME;
  2772. btrfs_balance_data(leaf, item, &disk_bargs);
  2773. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2774. btrfs_balance_meta(leaf, item, &disk_bargs);
  2775. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2776. btrfs_balance_sys(leaf, item, &disk_bargs);
  2777. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2778. WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
  2779. mutex_lock(&fs_info->volume_mutex);
  2780. mutex_lock(&fs_info->balance_mutex);
  2781. set_balance_control(bctl);
  2782. mutex_unlock(&fs_info->balance_mutex);
  2783. mutex_unlock(&fs_info->volume_mutex);
  2784. out:
  2785. btrfs_free_path(path);
  2786. return ret;
  2787. }
  2788. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  2789. {
  2790. int ret = 0;
  2791. mutex_lock(&fs_info->balance_mutex);
  2792. if (!fs_info->balance_ctl) {
  2793. mutex_unlock(&fs_info->balance_mutex);
  2794. return -ENOTCONN;
  2795. }
  2796. if (atomic_read(&fs_info->balance_running)) {
  2797. atomic_inc(&fs_info->balance_pause_req);
  2798. mutex_unlock(&fs_info->balance_mutex);
  2799. wait_event(fs_info->balance_wait_q,
  2800. atomic_read(&fs_info->balance_running) == 0);
  2801. mutex_lock(&fs_info->balance_mutex);
  2802. /* we are good with balance_ctl ripped off from under us */
  2803. BUG_ON(atomic_read(&fs_info->balance_running));
  2804. atomic_dec(&fs_info->balance_pause_req);
  2805. } else {
  2806. ret = -ENOTCONN;
  2807. }
  2808. mutex_unlock(&fs_info->balance_mutex);
  2809. return ret;
  2810. }
  2811. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  2812. {
  2813. mutex_lock(&fs_info->balance_mutex);
  2814. if (!fs_info->balance_ctl) {
  2815. mutex_unlock(&fs_info->balance_mutex);
  2816. return -ENOTCONN;
  2817. }
  2818. atomic_inc(&fs_info->balance_cancel_req);
  2819. /*
  2820. * if we are running just wait and return, balance item is
  2821. * deleted in btrfs_balance in this case
  2822. */
  2823. if (atomic_read(&fs_info->balance_running)) {
  2824. mutex_unlock(&fs_info->balance_mutex);
  2825. wait_event(fs_info->balance_wait_q,
  2826. atomic_read(&fs_info->balance_running) == 0);
  2827. mutex_lock(&fs_info->balance_mutex);
  2828. } else {
  2829. /* __cancel_balance needs volume_mutex */
  2830. mutex_unlock(&fs_info->balance_mutex);
  2831. mutex_lock(&fs_info->volume_mutex);
  2832. mutex_lock(&fs_info->balance_mutex);
  2833. if (fs_info->balance_ctl)
  2834. __cancel_balance(fs_info);
  2835. mutex_unlock(&fs_info->volume_mutex);
  2836. }
  2837. BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
  2838. atomic_dec(&fs_info->balance_cancel_req);
  2839. mutex_unlock(&fs_info->balance_mutex);
  2840. return 0;
  2841. }
  2842. /*
  2843. * shrinking a device means finding all of the device extents past
  2844. * the new size, and then following the back refs to the chunks.
  2845. * The chunk relocation code actually frees the device extent
  2846. */
  2847. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2848. {
  2849. struct btrfs_trans_handle *trans;
  2850. struct btrfs_root *root = device->dev_root;
  2851. struct btrfs_dev_extent *dev_extent = NULL;
  2852. struct btrfs_path *path;
  2853. u64 length;
  2854. u64 chunk_tree;
  2855. u64 chunk_objectid;
  2856. u64 chunk_offset;
  2857. int ret;
  2858. int slot;
  2859. int failed = 0;
  2860. bool retried = false;
  2861. struct extent_buffer *l;
  2862. struct btrfs_key key;
  2863. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2864. u64 old_total = btrfs_super_total_bytes(super_copy);
  2865. u64 old_size = device->total_bytes;
  2866. u64 diff = device->total_bytes - new_size;
  2867. if (device->is_tgtdev_for_dev_replace)
  2868. return -EINVAL;
  2869. path = btrfs_alloc_path();
  2870. if (!path)
  2871. return -ENOMEM;
  2872. path->reada = 2;
  2873. lock_chunks(root);
  2874. device->total_bytes = new_size;
  2875. if (device->writeable) {
  2876. device->fs_devices->total_rw_bytes -= diff;
  2877. spin_lock(&root->fs_info->free_chunk_lock);
  2878. root->fs_info->free_chunk_space -= diff;
  2879. spin_unlock(&root->fs_info->free_chunk_lock);
  2880. }
  2881. unlock_chunks(root);
  2882. again:
  2883. key.objectid = device->devid;
  2884. key.offset = (u64)-1;
  2885. key.type = BTRFS_DEV_EXTENT_KEY;
  2886. do {
  2887. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2888. if (ret < 0)
  2889. goto done;
  2890. ret = btrfs_previous_item(root, path, 0, key.type);
  2891. if (ret < 0)
  2892. goto done;
  2893. if (ret) {
  2894. ret = 0;
  2895. btrfs_release_path(path);
  2896. break;
  2897. }
  2898. l = path->nodes[0];
  2899. slot = path->slots[0];
  2900. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2901. if (key.objectid != device->devid) {
  2902. btrfs_release_path(path);
  2903. break;
  2904. }
  2905. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2906. length = btrfs_dev_extent_length(l, dev_extent);
  2907. if (key.offset + length <= new_size) {
  2908. btrfs_release_path(path);
  2909. break;
  2910. }
  2911. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2912. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2913. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2914. btrfs_release_path(path);
  2915. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2916. chunk_offset);
  2917. if (ret && ret != -ENOSPC)
  2918. goto done;
  2919. if (ret == -ENOSPC)
  2920. failed++;
  2921. } while (key.offset-- > 0);
  2922. if (failed && !retried) {
  2923. failed = 0;
  2924. retried = true;
  2925. goto again;
  2926. } else if (failed && retried) {
  2927. ret = -ENOSPC;
  2928. lock_chunks(root);
  2929. device->total_bytes = old_size;
  2930. if (device->writeable)
  2931. device->fs_devices->total_rw_bytes += diff;
  2932. spin_lock(&root->fs_info->free_chunk_lock);
  2933. root->fs_info->free_chunk_space += diff;
  2934. spin_unlock(&root->fs_info->free_chunk_lock);
  2935. unlock_chunks(root);
  2936. goto done;
  2937. }
  2938. /* Shrinking succeeded, else we would be at "done". */
  2939. trans = btrfs_start_transaction(root, 0);
  2940. if (IS_ERR(trans)) {
  2941. ret = PTR_ERR(trans);
  2942. goto done;
  2943. }
  2944. lock_chunks(root);
  2945. device->disk_total_bytes = new_size;
  2946. /* Now btrfs_update_device() will change the on-disk size. */
  2947. ret = btrfs_update_device(trans, device);
  2948. if (ret) {
  2949. unlock_chunks(root);
  2950. btrfs_end_transaction(trans, root);
  2951. goto done;
  2952. }
  2953. WARN_ON(diff > old_total);
  2954. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  2955. unlock_chunks(root);
  2956. btrfs_end_transaction(trans, root);
  2957. done:
  2958. btrfs_free_path(path);
  2959. return ret;
  2960. }
  2961. static int btrfs_add_system_chunk(struct btrfs_root *root,
  2962. struct btrfs_key *key,
  2963. struct btrfs_chunk *chunk, int item_size)
  2964. {
  2965. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2966. struct btrfs_disk_key disk_key;
  2967. u32 array_size;
  2968. u8 *ptr;
  2969. array_size = btrfs_super_sys_array_size(super_copy);
  2970. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2971. return -EFBIG;
  2972. ptr = super_copy->sys_chunk_array + array_size;
  2973. btrfs_cpu_key_to_disk(&disk_key, key);
  2974. memcpy(ptr, &disk_key, sizeof(disk_key));
  2975. ptr += sizeof(disk_key);
  2976. memcpy(ptr, chunk, item_size);
  2977. item_size += sizeof(disk_key);
  2978. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2979. return 0;
  2980. }
  2981. /*
  2982. * sort the devices in descending order by max_avail, total_avail
  2983. */
  2984. static int btrfs_cmp_device_info(const void *a, const void *b)
  2985. {
  2986. const struct btrfs_device_info *di_a = a;
  2987. const struct btrfs_device_info *di_b = b;
  2988. if (di_a->max_avail > di_b->max_avail)
  2989. return -1;
  2990. if (di_a->max_avail < di_b->max_avail)
  2991. return 1;
  2992. if (di_a->total_avail > di_b->total_avail)
  2993. return -1;
  2994. if (di_a->total_avail < di_b->total_avail)
  2995. return 1;
  2996. return 0;
  2997. }
  2998. struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
  2999. { 2, 1, 0, 4, 2, 2 /* raid10 */ },
  3000. { 1, 1, 2, 2, 2, 2 /* raid1 */ },
  3001. { 1, 2, 1, 1, 1, 2 /* dup */ },
  3002. { 1, 1, 0, 2, 1, 1 /* raid0 */ },
  3003. { 1, 1, 1, 1, 1, 1 /* single */ },
  3004. };
  3005. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3006. struct btrfs_root *extent_root,
  3007. struct map_lookup **map_ret,
  3008. u64 *num_bytes_out, u64 *stripe_size_out,
  3009. u64 start, u64 type)
  3010. {
  3011. struct btrfs_fs_info *info = extent_root->fs_info;
  3012. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  3013. struct list_head *cur;
  3014. struct map_lookup *map = NULL;
  3015. struct extent_map_tree *em_tree;
  3016. struct extent_map *em;
  3017. struct btrfs_device_info *devices_info = NULL;
  3018. u64 total_avail;
  3019. int num_stripes; /* total number of stripes to allocate */
  3020. int sub_stripes; /* sub_stripes info for map */
  3021. int dev_stripes; /* stripes per dev */
  3022. int devs_max; /* max devs to use */
  3023. int devs_min; /* min devs needed */
  3024. int devs_increment; /* ndevs has to be a multiple of this */
  3025. int ncopies; /* how many copies to data has */
  3026. int ret;
  3027. u64 max_stripe_size;
  3028. u64 max_chunk_size;
  3029. u64 stripe_size;
  3030. u64 num_bytes;
  3031. int ndevs;
  3032. int i;
  3033. int j;
  3034. int index;
  3035. BUG_ON(!alloc_profile_is_valid(type, 0));
  3036. if (list_empty(&fs_devices->alloc_list))
  3037. return -ENOSPC;
  3038. index = __get_raid_index(type);
  3039. sub_stripes = btrfs_raid_array[index].sub_stripes;
  3040. dev_stripes = btrfs_raid_array[index].dev_stripes;
  3041. devs_max = btrfs_raid_array[index].devs_max;
  3042. devs_min = btrfs_raid_array[index].devs_min;
  3043. devs_increment = btrfs_raid_array[index].devs_increment;
  3044. ncopies = btrfs_raid_array[index].ncopies;
  3045. if (type & BTRFS_BLOCK_GROUP_DATA) {
  3046. max_stripe_size = 1024 * 1024 * 1024;
  3047. max_chunk_size = 10 * max_stripe_size;
  3048. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  3049. /* for larger filesystems, use larger metadata chunks */
  3050. if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
  3051. max_stripe_size = 1024 * 1024 * 1024;
  3052. else
  3053. max_stripe_size = 256 * 1024 * 1024;
  3054. max_chunk_size = max_stripe_size;
  3055. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3056. max_stripe_size = 32 * 1024 * 1024;
  3057. max_chunk_size = 2 * max_stripe_size;
  3058. } else {
  3059. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  3060. type);
  3061. BUG_ON(1);
  3062. }
  3063. /* we don't want a chunk larger than 10% of writeable space */
  3064. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  3065. max_chunk_size);
  3066. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  3067. GFP_NOFS);
  3068. if (!devices_info)
  3069. return -ENOMEM;
  3070. cur = fs_devices->alloc_list.next;
  3071. /*
  3072. * in the first pass through the devices list, we gather information
  3073. * about the available holes on each device.
  3074. */
  3075. ndevs = 0;
  3076. while (cur != &fs_devices->alloc_list) {
  3077. struct btrfs_device *device;
  3078. u64 max_avail;
  3079. u64 dev_offset;
  3080. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  3081. cur = cur->next;
  3082. if (!device->writeable) {
  3083. WARN(1, KERN_ERR
  3084. "btrfs: read-only device in alloc_list\n");
  3085. continue;
  3086. }
  3087. if (!device->in_fs_metadata ||
  3088. device->is_tgtdev_for_dev_replace)
  3089. continue;
  3090. if (device->total_bytes > device->bytes_used)
  3091. total_avail = device->total_bytes - device->bytes_used;
  3092. else
  3093. total_avail = 0;
  3094. /* If there is no space on this device, skip it. */
  3095. if (total_avail == 0)
  3096. continue;
  3097. ret = find_free_dev_extent(device,
  3098. max_stripe_size * dev_stripes,
  3099. &dev_offset, &max_avail);
  3100. if (ret && ret != -ENOSPC)
  3101. goto error;
  3102. if (ret == 0)
  3103. max_avail = max_stripe_size * dev_stripes;
  3104. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  3105. continue;
  3106. devices_info[ndevs].dev_offset = dev_offset;
  3107. devices_info[ndevs].max_avail = max_avail;
  3108. devices_info[ndevs].total_avail = total_avail;
  3109. devices_info[ndevs].dev = device;
  3110. ++ndevs;
  3111. WARN_ON(ndevs > fs_devices->rw_devices);
  3112. }
  3113. /*
  3114. * now sort the devices by hole size / available space
  3115. */
  3116. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  3117. btrfs_cmp_device_info, NULL);
  3118. /* round down to number of usable stripes */
  3119. ndevs -= ndevs % devs_increment;
  3120. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  3121. ret = -ENOSPC;
  3122. goto error;
  3123. }
  3124. if (devs_max && ndevs > devs_max)
  3125. ndevs = devs_max;
  3126. /*
  3127. * the primary goal is to maximize the number of stripes, so use as many
  3128. * devices as possible, even if the stripes are not maximum sized.
  3129. */
  3130. stripe_size = devices_info[ndevs-1].max_avail;
  3131. num_stripes = ndevs * dev_stripes;
  3132. if (stripe_size * ndevs > max_chunk_size * ncopies) {
  3133. stripe_size = max_chunk_size * ncopies;
  3134. do_div(stripe_size, ndevs);
  3135. }
  3136. do_div(stripe_size, dev_stripes);
  3137. /* align to BTRFS_STRIPE_LEN */
  3138. do_div(stripe_size, BTRFS_STRIPE_LEN);
  3139. stripe_size *= BTRFS_STRIPE_LEN;
  3140. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3141. if (!map) {
  3142. ret = -ENOMEM;
  3143. goto error;
  3144. }
  3145. map->num_stripes = num_stripes;
  3146. for (i = 0; i < ndevs; ++i) {
  3147. for (j = 0; j < dev_stripes; ++j) {
  3148. int s = i * dev_stripes + j;
  3149. map->stripes[s].dev = devices_info[i].dev;
  3150. map->stripes[s].physical = devices_info[i].dev_offset +
  3151. j * stripe_size;
  3152. }
  3153. }
  3154. map->sector_size = extent_root->sectorsize;
  3155. map->stripe_len = BTRFS_STRIPE_LEN;
  3156. map->io_align = BTRFS_STRIPE_LEN;
  3157. map->io_width = BTRFS_STRIPE_LEN;
  3158. map->type = type;
  3159. map->sub_stripes = sub_stripes;
  3160. *map_ret = map;
  3161. num_bytes = stripe_size * (num_stripes / ncopies);
  3162. *stripe_size_out = stripe_size;
  3163. *num_bytes_out = num_bytes;
  3164. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  3165. em = alloc_extent_map();
  3166. if (!em) {
  3167. ret = -ENOMEM;
  3168. goto error;
  3169. }
  3170. em->bdev = (struct block_device *)map;
  3171. em->start = start;
  3172. em->len = num_bytes;
  3173. em->block_start = 0;
  3174. em->block_len = em->len;
  3175. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  3176. write_lock(&em_tree->lock);
  3177. ret = add_extent_mapping(em_tree, em);
  3178. write_unlock(&em_tree->lock);
  3179. free_extent_map(em);
  3180. if (ret)
  3181. goto error;
  3182. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  3183. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3184. start, num_bytes);
  3185. if (ret)
  3186. goto error;
  3187. for (i = 0; i < map->num_stripes; ++i) {
  3188. struct btrfs_device *device;
  3189. u64 dev_offset;
  3190. device = map->stripes[i].dev;
  3191. dev_offset = map->stripes[i].physical;
  3192. ret = btrfs_alloc_dev_extent(trans, device,
  3193. info->chunk_root->root_key.objectid,
  3194. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3195. start, dev_offset, stripe_size);
  3196. if (ret) {
  3197. btrfs_abort_transaction(trans, extent_root, ret);
  3198. goto error;
  3199. }
  3200. }
  3201. kfree(devices_info);
  3202. return 0;
  3203. error:
  3204. kfree(map);
  3205. kfree(devices_info);
  3206. return ret;
  3207. }
  3208. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  3209. struct btrfs_root *extent_root,
  3210. struct map_lookup *map, u64 chunk_offset,
  3211. u64 chunk_size, u64 stripe_size)
  3212. {
  3213. u64 dev_offset;
  3214. struct btrfs_key key;
  3215. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3216. struct btrfs_device *device;
  3217. struct btrfs_chunk *chunk;
  3218. struct btrfs_stripe *stripe;
  3219. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  3220. int index = 0;
  3221. int ret;
  3222. chunk = kzalloc(item_size, GFP_NOFS);
  3223. if (!chunk)
  3224. return -ENOMEM;
  3225. index = 0;
  3226. while (index < map->num_stripes) {
  3227. device = map->stripes[index].dev;
  3228. device->bytes_used += stripe_size;
  3229. ret = btrfs_update_device(trans, device);
  3230. if (ret)
  3231. goto out_free;
  3232. index++;
  3233. }
  3234. spin_lock(&extent_root->fs_info->free_chunk_lock);
  3235. extent_root->fs_info->free_chunk_space -= (stripe_size *
  3236. map->num_stripes);
  3237. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  3238. index = 0;
  3239. stripe = &chunk->stripe;
  3240. while (index < map->num_stripes) {
  3241. device = map->stripes[index].dev;
  3242. dev_offset = map->stripes[index].physical;
  3243. btrfs_set_stack_stripe_devid(stripe, device->devid);
  3244. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  3245. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  3246. stripe++;
  3247. index++;
  3248. }
  3249. btrfs_set_stack_chunk_length(chunk, chunk_size);
  3250. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  3251. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  3252. btrfs_set_stack_chunk_type(chunk, map->type);
  3253. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  3254. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  3255. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  3256. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  3257. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  3258. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3259. key.type = BTRFS_CHUNK_ITEM_KEY;
  3260. key.offset = chunk_offset;
  3261. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  3262. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3263. /*
  3264. * TODO: Cleanup of inserted chunk root in case of
  3265. * failure.
  3266. */
  3267. ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
  3268. item_size);
  3269. }
  3270. out_free:
  3271. kfree(chunk);
  3272. return ret;
  3273. }
  3274. /*
  3275. * Chunk allocation falls into two parts. The first part does works
  3276. * that make the new allocated chunk useable, but not do any operation
  3277. * that modifies the chunk tree. The second part does the works that
  3278. * require modifying the chunk tree. This division is important for the
  3279. * bootstrap process of adding storage to a seed btrfs.
  3280. */
  3281. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3282. struct btrfs_root *extent_root, u64 type)
  3283. {
  3284. u64 chunk_offset;
  3285. u64 chunk_size;
  3286. u64 stripe_size;
  3287. struct map_lookup *map;
  3288. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3289. int ret;
  3290. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3291. &chunk_offset);
  3292. if (ret)
  3293. return ret;
  3294. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3295. &stripe_size, chunk_offset, type);
  3296. if (ret)
  3297. return ret;
  3298. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3299. chunk_size, stripe_size);
  3300. if (ret)
  3301. return ret;
  3302. return 0;
  3303. }
  3304. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  3305. struct btrfs_root *root,
  3306. struct btrfs_device *device)
  3307. {
  3308. u64 chunk_offset;
  3309. u64 sys_chunk_offset;
  3310. u64 chunk_size;
  3311. u64 sys_chunk_size;
  3312. u64 stripe_size;
  3313. u64 sys_stripe_size;
  3314. u64 alloc_profile;
  3315. struct map_lookup *map;
  3316. struct map_lookup *sys_map;
  3317. struct btrfs_fs_info *fs_info = root->fs_info;
  3318. struct btrfs_root *extent_root = fs_info->extent_root;
  3319. int ret;
  3320. ret = find_next_chunk(fs_info->chunk_root,
  3321. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  3322. if (ret)
  3323. return ret;
  3324. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  3325. fs_info->avail_metadata_alloc_bits;
  3326. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  3327. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3328. &stripe_size, chunk_offset, alloc_profile);
  3329. if (ret)
  3330. return ret;
  3331. sys_chunk_offset = chunk_offset + chunk_size;
  3332. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  3333. fs_info->avail_system_alloc_bits;
  3334. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  3335. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  3336. &sys_chunk_size, &sys_stripe_size,
  3337. sys_chunk_offset, alloc_profile);
  3338. if (ret) {
  3339. btrfs_abort_transaction(trans, root, ret);
  3340. goto out;
  3341. }
  3342. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  3343. if (ret) {
  3344. btrfs_abort_transaction(trans, root, ret);
  3345. goto out;
  3346. }
  3347. /*
  3348. * Modifying chunk tree needs allocating new blocks from both
  3349. * system block group and metadata block group. So we only can
  3350. * do operations require modifying the chunk tree after both
  3351. * block groups were created.
  3352. */
  3353. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3354. chunk_size, stripe_size);
  3355. if (ret) {
  3356. btrfs_abort_transaction(trans, root, ret);
  3357. goto out;
  3358. }
  3359. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  3360. sys_chunk_offset, sys_chunk_size,
  3361. sys_stripe_size);
  3362. if (ret)
  3363. btrfs_abort_transaction(trans, root, ret);
  3364. out:
  3365. return ret;
  3366. }
  3367. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  3368. {
  3369. struct extent_map *em;
  3370. struct map_lookup *map;
  3371. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3372. int readonly = 0;
  3373. int i;
  3374. read_lock(&map_tree->map_tree.lock);
  3375. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  3376. read_unlock(&map_tree->map_tree.lock);
  3377. if (!em)
  3378. return 1;
  3379. if (btrfs_test_opt(root, DEGRADED)) {
  3380. free_extent_map(em);
  3381. return 0;
  3382. }
  3383. map = (struct map_lookup *)em->bdev;
  3384. for (i = 0; i < map->num_stripes; i++) {
  3385. if (!map->stripes[i].dev->writeable) {
  3386. readonly = 1;
  3387. break;
  3388. }
  3389. }
  3390. free_extent_map(em);
  3391. return readonly;
  3392. }
  3393. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  3394. {
  3395. extent_map_tree_init(&tree->map_tree);
  3396. }
  3397. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  3398. {
  3399. struct extent_map *em;
  3400. while (1) {
  3401. write_lock(&tree->map_tree.lock);
  3402. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  3403. if (em)
  3404. remove_extent_mapping(&tree->map_tree, em);
  3405. write_unlock(&tree->map_tree.lock);
  3406. if (!em)
  3407. break;
  3408. kfree(em->bdev);
  3409. /* once for us */
  3410. free_extent_map(em);
  3411. /* once for the tree */
  3412. free_extent_map(em);
  3413. }
  3414. }
  3415. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  3416. {
  3417. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3418. struct extent_map *em;
  3419. struct map_lookup *map;
  3420. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3421. int ret;
  3422. read_lock(&em_tree->lock);
  3423. em = lookup_extent_mapping(em_tree, logical, len);
  3424. read_unlock(&em_tree->lock);
  3425. BUG_ON(!em);
  3426. BUG_ON(em->start > logical || em->start + em->len < logical);
  3427. map = (struct map_lookup *)em->bdev;
  3428. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  3429. ret = map->num_stripes;
  3430. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3431. ret = map->sub_stripes;
  3432. else
  3433. ret = 1;
  3434. free_extent_map(em);
  3435. btrfs_dev_replace_lock(&fs_info->dev_replace);
  3436. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
  3437. ret++;
  3438. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  3439. return ret;
  3440. }
  3441. static int find_live_mirror(struct btrfs_fs_info *fs_info,
  3442. struct map_lookup *map, int first, int num,
  3443. int optimal, int dev_replace_is_ongoing)
  3444. {
  3445. int i;
  3446. int tolerance;
  3447. struct btrfs_device *srcdev;
  3448. if (dev_replace_is_ongoing &&
  3449. fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  3450. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
  3451. srcdev = fs_info->dev_replace.srcdev;
  3452. else
  3453. srcdev = NULL;
  3454. /*
  3455. * try to avoid the drive that is the source drive for a
  3456. * dev-replace procedure, only choose it if no other non-missing
  3457. * mirror is available
  3458. */
  3459. for (tolerance = 0; tolerance < 2; tolerance++) {
  3460. if (map->stripes[optimal].dev->bdev &&
  3461. (tolerance || map->stripes[optimal].dev != srcdev))
  3462. return optimal;
  3463. for (i = first; i < first + num; i++) {
  3464. if (map->stripes[i].dev->bdev &&
  3465. (tolerance || map->stripes[i].dev != srcdev))
  3466. return i;
  3467. }
  3468. }
  3469. /* we couldn't find one that doesn't fail. Just return something
  3470. * and the io error handling code will clean up eventually
  3471. */
  3472. return optimal;
  3473. }
  3474. static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  3475. u64 logical, u64 *length,
  3476. struct btrfs_bio **bbio_ret,
  3477. int mirror_num)
  3478. {
  3479. struct extent_map *em;
  3480. struct map_lookup *map;
  3481. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3482. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3483. u64 offset;
  3484. u64 stripe_offset;
  3485. u64 stripe_end_offset;
  3486. u64 stripe_nr;
  3487. u64 stripe_nr_orig;
  3488. u64 stripe_nr_end;
  3489. int stripe_index;
  3490. int i;
  3491. int ret = 0;
  3492. int num_stripes;
  3493. int max_errors = 0;
  3494. struct btrfs_bio *bbio = NULL;
  3495. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3496. int dev_replace_is_ongoing = 0;
  3497. int num_alloc_stripes;
  3498. int patch_the_first_stripe_for_dev_replace = 0;
  3499. u64 physical_to_patch_in_first_stripe = 0;
  3500. read_lock(&em_tree->lock);
  3501. em = lookup_extent_mapping(em_tree, logical, *length);
  3502. read_unlock(&em_tree->lock);
  3503. if (!em) {
  3504. printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
  3505. (unsigned long long)logical,
  3506. (unsigned long long)*length);
  3507. BUG();
  3508. }
  3509. BUG_ON(em->start > logical || em->start + em->len < logical);
  3510. map = (struct map_lookup *)em->bdev;
  3511. offset = logical - em->start;
  3512. stripe_nr = offset;
  3513. /*
  3514. * stripe_nr counts the total number of stripes we have to stride
  3515. * to get to this block
  3516. */
  3517. do_div(stripe_nr, map->stripe_len);
  3518. stripe_offset = stripe_nr * map->stripe_len;
  3519. BUG_ON(offset < stripe_offset);
  3520. /* stripe_offset is the offset of this block in its stripe*/
  3521. stripe_offset = offset - stripe_offset;
  3522. if (rw & REQ_DISCARD)
  3523. *length = min_t(u64, em->len - offset, *length);
  3524. else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  3525. /* we limit the length of each bio to what fits in a stripe */
  3526. *length = min_t(u64, em->len - offset,
  3527. map->stripe_len - stripe_offset);
  3528. } else {
  3529. *length = em->len - offset;
  3530. }
  3531. if (!bbio_ret)
  3532. goto out;
  3533. btrfs_dev_replace_lock(dev_replace);
  3534. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  3535. if (!dev_replace_is_ongoing)
  3536. btrfs_dev_replace_unlock(dev_replace);
  3537. if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
  3538. !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
  3539. dev_replace->tgtdev != NULL) {
  3540. /*
  3541. * in dev-replace case, for repair case (that's the only
  3542. * case where the mirror is selected explicitly when
  3543. * calling btrfs_map_block), blocks left of the left cursor
  3544. * can also be read from the target drive.
  3545. * For REQ_GET_READ_MIRRORS, the target drive is added as
  3546. * the last one to the array of stripes. For READ, it also
  3547. * needs to be supported using the same mirror number.
  3548. * If the requested block is not left of the left cursor,
  3549. * EIO is returned. This can happen because btrfs_num_copies()
  3550. * returns one more in the dev-replace case.
  3551. */
  3552. u64 tmp_length = *length;
  3553. struct btrfs_bio *tmp_bbio = NULL;
  3554. int tmp_num_stripes;
  3555. u64 srcdev_devid = dev_replace->srcdev->devid;
  3556. int index_srcdev = 0;
  3557. int found = 0;
  3558. u64 physical_of_found = 0;
  3559. ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
  3560. logical, &tmp_length, &tmp_bbio, 0);
  3561. if (ret) {
  3562. WARN_ON(tmp_bbio != NULL);
  3563. goto out;
  3564. }
  3565. tmp_num_stripes = tmp_bbio->num_stripes;
  3566. if (mirror_num > tmp_num_stripes) {
  3567. /*
  3568. * REQ_GET_READ_MIRRORS does not contain this
  3569. * mirror, that means that the requested area
  3570. * is not left of the left cursor
  3571. */
  3572. ret = -EIO;
  3573. kfree(tmp_bbio);
  3574. goto out;
  3575. }
  3576. /*
  3577. * process the rest of the function using the mirror_num
  3578. * of the source drive. Therefore look it up first.
  3579. * At the end, patch the device pointer to the one of the
  3580. * target drive.
  3581. */
  3582. for (i = 0; i < tmp_num_stripes; i++) {
  3583. if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
  3584. /*
  3585. * In case of DUP, in order to keep it
  3586. * simple, only add the mirror with the
  3587. * lowest physical address
  3588. */
  3589. if (found &&
  3590. physical_of_found <=
  3591. tmp_bbio->stripes[i].physical)
  3592. continue;
  3593. index_srcdev = i;
  3594. found = 1;
  3595. physical_of_found =
  3596. tmp_bbio->stripes[i].physical;
  3597. }
  3598. }
  3599. if (found) {
  3600. mirror_num = index_srcdev + 1;
  3601. patch_the_first_stripe_for_dev_replace = 1;
  3602. physical_to_patch_in_first_stripe = physical_of_found;
  3603. } else {
  3604. WARN_ON(1);
  3605. ret = -EIO;
  3606. kfree(tmp_bbio);
  3607. goto out;
  3608. }
  3609. kfree(tmp_bbio);
  3610. } else if (mirror_num > map->num_stripes) {
  3611. mirror_num = 0;
  3612. }
  3613. num_stripes = 1;
  3614. stripe_index = 0;
  3615. stripe_nr_orig = stripe_nr;
  3616. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  3617. (~(map->stripe_len - 1));
  3618. do_div(stripe_nr_end, map->stripe_len);
  3619. stripe_end_offset = stripe_nr_end * map->stripe_len -
  3620. (offset + *length);
  3621. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3622. if (rw & REQ_DISCARD)
  3623. num_stripes = min_t(u64, map->num_stripes,
  3624. stripe_nr_end - stripe_nr_orig);
  3625. stripe_index = do_div(stripe_nr, map->num_stripes);
  3626. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  3627. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
  3628. num_stripes = map->num_stripes;
  3629. else if (mirror_num)
  3630. stripe_index = mirror_num - 1;
  3631. else {
  3632. stripe_index = find_live_mirror(fs_info, map, 0,
  3633. map->num_stripes,
  3634. current->pid % map->num_stripes,
  3635. dev_replace_is_ongoing);
  3636. mirror_num = stripe_index + 1;
  3637. }
  3638. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  3639. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
  3640. num_stripes = map->num_stripes;
  3641. } else if (mirror_num) {
  3642. stripe_index = mirror_num - 1;
  3643. } else {
  3644. mirror_num = 1;
  3645. }
  3646. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3647. int factor = map->num_stripes / map->sub_stripes;
  3648. stripe_index = do_div(stripe_nr, factor);
  3649. stripe_index *= map->sub_stripes;
  3650. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
  3651. num_stripes = map->sub_stripes;
  3652. else if (rw & REQ_DISCARD)
  3653. num_stripes = min_t(u64, map->sub_stripes *
  3654. (stripe_nr_end - stripe_nr_orig),
  3655. map->num_stripes);
  3656. else if (mirror_num)
  3657. stripe_index += mirror_num - 1;
  3658. else {
  3659. int old_stripe_index = stripe_index;
  3660. stripe_index = find_live_mirror(fs_info, map,
  3661. stripe_index,
  3662. map->sub_stripes, stripe_index +
  3663. current->pid % map->sub_stripes,
  3664. dev_replace_is_ongoing);
  3665. mirror_num = stripe_index - old_stripe_index + 1;
  3666. }
  3667. } else {
  3668. /*
  3669. * after this do_div call, stripe_nr is the number of stripes
  3670. * on this device we have to walk to find the data, and
  3671. * stripe_index is the number of our device in the stripe array
  3672. */
  3673. stripe_index = do_div(stripe_nr, map->num_stripes);
  3674. mirror_num = stripe_index + 1;
  3675. }
  3676. BUG_ON(stripe_index >= map->num_stripes);
  3677. num_alloc_stripes = num_stripes;
  3678. if (dev_replace_is_ongoing) {
  3679. if (rw & (REQ_WRITE | REQ_DISCARD))
  3680. num_alloc_stripes <<= 1;
  3681. if (rw & REQ_GET_READ_MIRRORS)
  3682. num_alloc_stripes++;
  3683. }
  3684. bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
  3685. if (!bbio) {
  3686. ret = -ENOMEM;
  3687. goto out;
  3688. }
  3689. atomic_set(&bbio->error, 0);
  3690. if (rw & REQ_DISCARD) {
  3691. int factor = 0;
  3692. int sub_stripes = 0;
  3693. u64 stripes_per_dev = 0;
  3694. u32 remaining_stripes = 0;
  3695. u32 last_stripe = 0;
  3696. if (map->type &
  3697. (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
  3698. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3699. sub_stripes = 1;
  3700. else
  3701. sub_stripes = map->sub_stripes;
  3702. factor = map->num_stripes / sub_stripes;
  3703. stripes_per_dev = div_u64_rem(stripe_nr_end -
  3704. stripe_nr_orig,
  3705. factor,
  3706. &remaining_stripes);
  3707. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  3708. last_stripe *= sub_stripes;
  3709. }
  3710. for (i = 0; i < num_stripes; i++) {
  3711. bbio->stripes[i].physical =
  3712. map->stripes[stripe_index].physical +
  3713. stripe_offset + stripe_nr * map->stripe_len;
  3714. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  3715. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  3716. BTRFS_BLOCK_GROUP_RAID10)) {
  3717. bbio->stripes[i].length = stripes_per_dev *
  3718. map->stripe_len;
  3719. if (i / sub_stripes < remaining_stripes)
  3720. bbio->stripes[i].length +=
  3721. map->stripe_len;
  3722. /*
  3723. * Special for the first stripe and
  3724. * the last stripe:
  3725. *
  3726. * |-------|...|-------|
  3727. * |----------|
  3728. * off end_off
  3729. */
  3730. if (i < sub_stripes)
  3731. bbio->stripes[i].length -=
  3732. stripe_offset;
  3733. if (stripe_index >= last_stripe &&
  3734. stripe_index <= (last_stripe +
  3735. sub_stripes - 1))
  3736. bbio->stripes[i].length -=
  3737. stripe_end_offset;
  3738. if (i == sub_stripes - 1)
  3739. stripe_offset = 0;
  3740. } else
  3741. bbio->stripes[i].length = *length;
  3742. stripe_index++;
  3743. if (stripe_index == map->num_stripes) {
  3744. /* This could only happen for RAID0/10 */
  3745. stripe_index = 0;
  3746. stripe_nr++;
  3747. }
  3748. }
  3749. } else {
  3750. for (i = 0; i < num_stripes; i++) {
  3751. bbio->stripes[i].physical =
  3752. map->stripes[stripe_index].physical +
  3753. stripe_offset +
  3754. stripe_nr * map->stripe_len;
  3755. bbio->stripes[i].dev =
  3756. map->stripes[stripe_index].dev;
  3757. stripe_index++;
  3758. }
  3759. }
  3760. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
  3761. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  3762. BTRFS_BLOCK_GROUP_RAID10 |
  3763. BTRFS_BLOCK_GROUP_DUP)) {
  3764. max_errors = 1;
  3765. }
  3766. }
  3767. if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  3768. dev_replace->tgtdev != NULL) {
  3769. int index_where_to_add;
  3770. u64 srcdev_devid = dev_replace->srcdev->devid;
  3771. /*
  3772. * duplicate the write operations while the dev replace
  3773. * procedure is running. Since the copying of the old disk
  3774. * to the new disk takes place at run time while the
  3775. * filesystem is mounted writable, the regular write
  3776. * operations to the old disk have to be duplicated to go
  3777. * to the new disk as well.
  3778. * Note that device->missing is handled by the caller, and
  3779. * that the write to the old disk is already set up in the
  3780. * stripes array.
  3781. */
  3782. index_where_to_add = num_stripes;
  3783. for (i = 0; i < num_stripes; i++) {
  3784. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  3785. /* write to new disk, too */
  3786. struct btrfs_bio_stripe *new =
  3787. bbio->stripes + index_where_to_add;
  3788. struct btrfs_bio_stripe *old =
  3789. bbio->stripes + i;
  3790. new->physical = old->physical;
  3791. new->length = old->length;
  3792. new->dev = dev_replace->tgtdev;
  3793. index_where_to_add++;
  3794. max_errors++;
  3795. }
  3796. }
  3797. num_stripes = index_where_to_add;
  3798. } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
  3799. dev_replace->tgtdev != NULL) {
  3800. u64 srcdev_devid = dev_replace->srcdev->devid;
  3801. int index_srcdev = 0;
  3802. int found = 0;
  3803. u64 physical_of_found = 0;
  3804. /*
  3805. * During the dev-replace procedure, the target drive can
  3806. * also be used to read data in case it is needed to repair
  3807. * a corrupt block elsewhere. This is possible if the
  3808. * requested area is left of the left cursor. In this area,
  3809. * the target drive is a full copy of the source drive.
  3810. */
  3811. for (i = 0; i < num_stripes; i++) {
  3812. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  3813. /*
  3814. * In case of DUP, in order to keep it
  3815. * simple, only add the mirror with the
  3816. * lowest physical address
  3817. */
  3818. if (found &&
  3819. physical_of_found <=
  3820. bbio->stripes[i].physical)
  3821. continue;
  3822. index_srcdev = i;
  3823. found = 1;
  3824. physical_of_found = bbio->stripes[i].physical;
  3825. }
  3826. }
  3827. if (found) {
  3828. u64 length = map->stripe_len;
  3829. if (physical_of_found + length <=
  3830. dev_replace->cursor_left) {
  3831. struct btrfs_bio_stripe *tgtdev_stripe =
  3832. bbio->stripes + num_stripes;
  3833. tgtdev_stripe->physical = physical_of_found;
  3834. tgtdev_stripe->length =
  3835. bbio->stripes[index_srcdev].length;
  3836. tgtdev_stripe->dev = dev_replace->tgtdev;
  3837. num_stripes++;
  3838. }
  3839. }
  3840. }
  3841. *bbio_ret = bbio;
  3842. bbio->num_stripes = num_stripes;
  3843. bbio->max_errors = max_errors;
  3844. bbio->mirror_num = mirror_num;
  3845. /*
  3846. * this is the case that REQ_READ && dev_replace_is_ongoing &&
  3847. * mirror_num == num_stripes + 1 && dev_replace target drive is
  3848. * available as a mirror
  3849. */
  3850. if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
  3851. WARN_ON(num_stripes > 1);
  3852. bbio->stripes[0].dev = dev_replace->tgtdev;
  3853. bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
  3854. bbio->mirror_num = map->num_stripes + 1;
  3855. }
  3856. out:
  3857. if (dev_replace_is_ongoing)
  3858. btrfs_dev_replace_unlock(dev_replace);
  3859. free_extent_map(em);
  3860. return ret;
  3861. }
  3862. int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  3863. u64 logical, u64 *length,
  3864. struct btrfs_bio **bbio_ret, int mirror_num)
  3865. {
  3866. return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
  3867. mirror_num);
  3868. }
  3869. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  3870. u64 chunk_start, u64 physical, u64 devid,
  3871. u64 **logical, int *naddrs, int *stripe_len)
  3872. {
  3873. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3874. struct extent_map *em;
  3875. struct map_lookup *map;
  3876. u64 *buf;
  3877. u64 bytenr;
  3878. u64 length;
  3879. u64 stripe_nr;
  3880. int i, j, nr = 0;
  3881. read_lock(&em_tree->lock);
  3882. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  3883. read_unlock(&em_tree->lock);
  3884. BUG_ON(!em || em->start != chunk_start);
  3885. map = (struct map_lookup *)em->bdev;
  3886. length = em->len;
  3887. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3888. do_div(length, map->num_stripes / map->sub_stripes);
  3889. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3890. do_div(length, map->num_stripes);
  3891. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  3892. BUG_ON(!buf); /* -ENOMEM */
  3893. for (i = 0; i < map->num_stripes; i++) {
  3894. if (devid && map->stripes[i].dev->devid != devid)
  3895. continue;
  3896. if (map->stripes[i].physical > physical ||
  3897. map->stripes[i].physical + length <= physical)
  3898. continue;
  3899. stripe_nr = physical - map->stripes[i].physical;
  3900. do_div(stripe_nr, map->stripe_len);
  3901. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3902. stripe_nr = stripe_nr * map->num_stripes + i;
  3903. do_div(stripe_nr, map->sub_stripes);
  3904. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3905. stripe_nr = stripe_nr * map->num_stripes + i;
  3906. }
  3907. bytenr = chunk_start + stripe_nr * map->stripe_len;
  3908. WARN_ON(nr >= map->num_stripes);
  3909. for (j = 0; j < nr; j++) {
  3910. if (buf[j] == bytenr)
  3911. break;
  3912. }
  3913. if (j == nr) {
  3914. WARN_ON(nr >= map->num_stripes);
  3915. buf[nr++] = bytenr;
  3916. }
  3917. }
  3918. *logical = buf;
  3919. *naddrs = nr;
  3920. *stripe_len = map->stripe_len;
  3921. free_extent_map(em);
  3922. return 0;
  3923. }
  3924. static void *merge_stripe_index_into_bio_private(void *bi_private,
  3925. unsigned int stripe_index)
  3926. {
  3927. /*
  3928. * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
  3929. * at most 1.
  3930. * The alternative solution (instead of stealing bits from the
  3931. * pointer) would be to allocate an intermediate structure
  3932. * that contains the old private pointer plus the stripe_index.
  3933. */
  3934. BUG_ON((((uintptr_t)bi_private) & 3) != 0);
  3935. BUG_ON(stripe_index > 3);
  3936. return (void *)(((uintptr_t)bi_private) | stripe_index);
  3937. }
  3938. static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
  3939. {
  3940. return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
  3941. }
  3942. static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
  3943. {
  3944. return (unsigned int)((uintptr_t)bi_private) & 3;
  3945. }
  3946. static void btrfs_end_bio(struct bio *bio, int err)
  3947. {
  3948. struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
  3949. int is_orig_bio = 0;
  3950. if (err) {
  3951. atomic_inc(&bbio->error);
  3952. if (err == -EIO || err == -EREMOTEIO) {
  3953. unsigned int stripe_index =
  3954. extract_stripe_index_from_bio_private(
  3955. bio->bi_private);
  3956. struct btrfs_device *dev;
  3957. BUG_ON(stripe_index >= bbio->num_stripes);
  3958. dev = bbio->stripes[stripe_index].dev;
  3959. if (dev->bdev) {
  3960. if (bio->bi_rw & WRITE)
  3961. btrfs_dev_stat_inc(dev,
  3962. BTRFS_DEV_STAT_WRITE_ERRS);
  3963. else
  3964. btrfs_dev_stat_inc(dev,
  3965. BTRFS_DEV_STAT_READ_ERRS);
  3966. if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
  3967. btrfs_dev_stat_inc(dev,
  3968. BTRFS_DEV_STAT_FLUSH_ERRS);
  3969. btrfs_dev_stat_print_on_error(dev);
  3970. }
  3971. }
  3972. }
  3973. if (bio == bbio->orig_bio)
  3974. is_orig_bio = 1;
  3975. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3976. if (!is_orig_bio) {
  3977. bio_put(bio);
  3978. bio = bbio->orig_bio;
  3979. }
  3980. bio->bi_private = bbio->private;
  3981. bio->bi_end_io = bbio->end_io;
  3982. bio->bi_bdev = (struct block_device *)
  3983. (unsigned long)bbio->mirror_num;
  3984. /* only send an error to the higher layers if it is
  3985. * beyond the tolerance of the multi-bio
  3986. */
  3987. if (atomic_read(&bbio->error) > bbio->max_errors) {
  3988. err = -EIO;
  3989. } else {
  3990. /*
  3991. * this bio is actually up to date, we didn't
  3992. * go over the max number of errors
  3993. */
  3994. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3995. err = 0;
  3996. }
  3997. kfree(bbio);
  3998. bio_endio(bio, err);
  3999. } else if (!is_orig_bio) {
  4000. bio_put(bio);
  4001. }
  4002. }
  4003. struct async_sched {
  4004. struct bio *bio;
  4005. int rw;
  4006. struct btrfs_fs_info *info;
  4007. struct btrfs_work work;
  4008. };
  4009. /*
  4010. * see run_scheduled_bios for a description of why bios are collected for
  4011. * async submit.
  4012. *
  4013. * This will add one bio to the pending list for a device and make sure
  4014. * the work struct is scheduled.
  4015. */
  4016. static noinline void schedule_bio(struct btrfs_root *root,
  4017. struct btrfs_device *device,
  4018. int rw, struct bio *bio)
  4019. {
  4020. int should_queue = 1;
  4021. struct btrfs_pending_bios *pending_bios;
  4022. /* don't bother with additional async steps for reads, right now */
  4023. if (!(rw & REQ_WRITE)) {
  4024. bio_get(bio);
  4025. btrfsic_submit_bio(rw, bio);
  4026. bio_put(bio);
  4027. return;
  4028. }
  4029. /*
  4030. * nr_async_bios allows us to reliably return congestion to the
  4031. * higher layers. Otherwise, the async bio makes it appear we have
  4032. * made progress against dirty pages when we've really just put it
  4033. * on a queue for later
  4034. */
  4035. atomic_inc(&root->fs_info->nr_async_bios);
  4036. WARN_ON(bio->bi_next);
  4037. bio->bi_next = NULL;
  4038. bio->bi_rw |= rw;
  4039. spin_lock(&device->io_lock);
  4040. if (bio->bi_rw & REQ_SYNC)
  4041. pending_bios = &device->pending_sync_bios;
  4042. else
  4043. pending_bios = &device->pending_bios;
  4044. if (pending_bios->tail)
  4045. pending_bios->tail->bi_next = bio;
  4046. pending_bios->tail = bio;
  4047. if (!pending_bios->head)
  4048. pending_bios->head = bio;
  4049. if (device->running_pending)
  4050. should_queue = 0;
  4051. spin_unlock(&device->io_lock);
  4052. if (should_queue)
  4053. btrfs_queue_worker(&root->fs_info->submit_workers,
  4054. &device->work);
  4055. }
  4056. static int bio_size_ok(struct block_device *bdev, struct bio *bio,
  4057. sector_t sector)
  4058. {
  4059. struct bio_vec *prev;
  4060. struct request_queue *q = bdev_get_queue(bdev);
  4061. unsigned short max_sectors = queue_max_sectors(q);
  4062. struct bvec_merge_data bvm = {
  4063. .bi_bdev = bdev,
  4064. .bi_sector = sector,
  4065. .bi_rw = bio->bi_rw,
  4066. };
  4067. if (bio->bi_vcnt == 0) {
  4068. WARN_ON(1);
  4069. return 1;
  4070. }
  4071. prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  4072. if ((bio->bi_size >> 9) > max_sectors)
  4073. return 0;
  4074. if (!q->merge_bvec_fn)
  4075. return 1;
  4076. bvm.bi_size = bio->bi_size - prev->bv_len;
  4077. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
  4078. return 0;
  4079. return 1;
  4080. }
  4081. static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4082. struct bio *bio, u64 physical, int dev_nr,
  4083. int rw, int async)
  4084. {
  4085. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  4086. bio->bi_private = bbio;
  4087. bio->bi_private = merge_stripe_index_into_bio_private(
  4088. bio->bi_private, (unsigned int)dev_nr);
  4089. bio->bi_end_io = btrfs_end_bio;
  4090. bio->bi_sector = physical >> 9;
  4091. #ifdef DEBUG
  4092. {
  4093. struct rcu_string *name;
  4094. rcu_read_lock();
  4095. name = rcu_dereference(dev->name);
  4096. pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
  4097. "(%s id %llu), size=%u\n", rw,
  4098. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  4099. name->str, dev->devid, bio->bi_size);
  4100. rcu_read_unlock();
  4101. }
  4102. #endif
  4103. bio->bi_bdev = dev->bdev;
  4104. if (async)
  4105. schedule_bio(root, dev, rw, bio);
  4106. else
  4107. btrfsic_submit_bio(rw, bio);
  4108. }
  4109. static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4110. struct bio *first_bio, struct btrfs_device *dev,
  4111. int dev_nr, int rw, int async)
  4112. {
  4113. struct bio_vec *bvec = first_bio->bi_io_vec;
  4114. struct bio *bio;
  4115. int nr_vecs = bio_get_nr_vecs(dev->bdev);
  4116. u64 physical = bbio->stripes[dev_nr].physical;
  4117. again:
  4118. bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
  4119. if (!bio)
  4120. return -ENOMEM;
  4121. while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
  4122. if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  4123. bvec->bv_offset) < bvec->bv_len) {
  4124. u64 len = bio->bi_size;
  4125. atomic_inc(&bbio->stripes_pending);
  4126. submit_stripe_bio(root, bbio, bio, physical, dev_nr,
  4127. rw, async);
  4128. physical += len;
  4129. goto again;
  4130. }
  4131. bvec++;
  4132. }
  4133. submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
  4134. return 0;
  4135. }
  4136. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  4137. {
  4138. atomic_inc(&bbio->error);
  4139. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4140. bio->bi_private = bbio->private;
  4141. bio->bi_end_io = bbio->end_io;
  4142. bio->bi_bdev = (struct block_device *)
  4143. (unsigned long)bbio->mirror_num;
  4144. bio->bi_sector = logical >> 9;
  4145. kfree(bbio);
  4146. bio_endio(bio, -EIO);
  4147. }
  4148. }
  4149. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  4150. int mirror_num, int async_submit)
  4151. {
  4152. struct btrfs_device *dev;
  4153. struct bio *first_bio = bio;
  4154. u64 logical = (u64)bio->bi_sector << 9;
  4155. u64 length = 0;
  4156. u64 map_length;
  4157. int ret;
  4158. int dev_nr = 0;
  4159. int total_devs = 1;
  4160. struct btrfs_bio *bbio = NULL;
  4161. length = bio->bi_size;
  4162. map_length = length;
  4163. ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
  4164. mirror_num);
  4165. if (ret)
  4166. return ret;
  4167. total_devs = bbio->num_stripes;
  4168. if (map_length < length) {
  4169. printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
  4170. "len %llu\n", (unsigned long long)logical,
  4171. (unsigned long long)length,
  4172. (unsigned long long)map_length);
  4173. BUG();
  4174. }
  4175. bbio->orig_bio = first_bio;
  4176. bbio->private = first_bio->bi_private;
  4177. bbio->end_io = first_bio->bi_end_io;
  4178. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  4179. while (dev_nr < total_devs) {
  4180. dev = bbio->stripes[dev_nr].dev;
  4181. if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
  4182. bbio_error(bbio, first_bio, logical);
  4183. dev_nr++;
  4184. continue;
  4185. }
  4186. /*
  4187. * Check and see if we're ok with this bio based on it's size
  4188. * and offset with the given device.
  4189. */
  4190. if (!bio_size_ok(dev->bdev, first_bio,
  4191. bbio->stripes[dev_nr].physical >> 9)) {
  4192. ret = breakup_stripe_bio(root, bbio, first_bio, dev,
  4193. dev_nr, rw, async_submit);
  4194. BUG_ON(ret);
  4195. dev_nr++;
  4196. continue;
  4197. }
  4198. if (dev_nr < total_devs - 1) {
  4199. bio = bio_clone(first_bio, GFP_NOFS);
  4200. BUG_ON(!bio); /* -ENOMEM */
  4201. } else {
  4202. bio = first_bio;
  4203. }
  4204. submit_stripe_bio(root, bbio, bio,
  4205. bbio->stripes[dev_nr].physical, dev_nr, rw,
  4206. async_submit);
  4207. dev_nr++;
  4208. }
  4209. return 0;
  4210. }
  4211. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  4212. u8 *uuid, u8 *fsid)
  4213. {
  4214. struct btrfs_device *device;
  4215. struct btrfs_fs_devices *cur_devices;
  4216. cur_devices = fs_info->fs_devices;
  4217. while (cur_devices) {
  4218. if (!fsid ||
  4219. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4220. device = __find_device(&cur_devices->devices,
  4221. devid, uuid);
  4222. if (device)
  4223. return device;
  4224. }
  4225. cur_devices = cur_devices->seed;
  4226. }
  4227. return NULL;
  4228. }
  4229. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  4230. u64 devid, u8 *dev_uuid)
  4231. {
  4232. struct btrfs_device *device;
  4233. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  4234. device = kzalloc(sizeof(*device), GFP_NOFS);
  4235. if (!device)
  4236. return NULL;
  4237. list_add(&device->dev_list,
  4238. &fs_devices->devices);
  4239. device->dev_root = root->fs_info->dev_root;
  4240. device->devid = devid;
  4241. device->work.func = pending_bios_fn;
  4242. device->fs_devices = fs_devices;
  4243. device->missing = 1;
  4244. fs_devices->num_devices++;
  4245. fs_devices->missing_devices++;
  4246. spin_lock_init(&device->io_lock);
  4247. INIT_LIST_HEAD(&device->dev_alloc_list);
  4248. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  4249. return device;
  4250. }
  4251. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  4252. struct extent_buffer *leaf,
  4253. struct btrfs_chunk *chunk)
  4254. {
  4255. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  4256. struct map_lookup *map;
  4257. struct extent_map *em;
  4258. u64 logical;
  4259. u64 length;
  4260. u64 devid;
  4261. u8 uuid[BTRFS_UUID_SIZE];
  4262. int num_stripes;
  4263. int ret;
  4264. int i;
  4265. logical = key->offset;
  4266. length = btrfs_chunk_length(leaf, chunk);
  4267. read_lock(&map_tree->map_tree.lock);
  4268. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  4269. read_unlock(&map_tree->map_tree.lock);
  4270. /* already mapped? */
  4271. if (em && em->start <= logical && em->start + em->len > logical) {
  4272. free_extent_map(em);
  4273. return 0;
  4274. } else if (em) {
  4275. free_extent_map(em);
  4276. }
  4277. em = alloc_extent_map();
  4278. if (!em)
  4279. return -ENOMEM;
  4280. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  4281. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  4282. if (!map) {
  4283. free_extent_map(em);
  4284. return -ENOMEM;
  4285. }
  4286. em->bdev = (struct block_device *)map;
  4287. em->start = logical;
  4288. em->len = length;
  4289. em->orig_start = 0;
  4290. em->block_start = 0;
  4291. em->block_len = em->len;
  4292. map->num_stripes = num_stripes;
  4293. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  4294. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  4295. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  4296. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  4297. map->type = btrfs_chunk_type(leaf, chunk);
  4298. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  4299. for (i = 0; i < num_stripes; i++) {
  4300. map->stripes[i].physical =
  4301. btrfs_stripe_offset_nr(leaf, chunk, i);
  4302. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  4303. read_extent_buffer(leaf, uuid, (unsigned long)
  4304. btrfs_stripe_dev_uuid_nr(chunk, i),
  4305. BTRFS_UUID_SIZE);
  4306. map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
  4307. uuid, NULL);
  4308. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  4309. kfree(map);
  4310. free_extent_map(em);
  4311. return -EIO;
  4312. }
  4313. if (!map->stripes[i].dev) {
  4314. map->stripes[i].dev =
  4315. add_missing_dev(root, devid, uuid);
  4316. if (!map->stripes[i].dev) {
  4317. kfree(map);
  4318. free_extent_map(em);
  4319. return -EIO;
  4320. }
  4321. }
  4322. map->stripes[i].dev->in_fs_metadata = 1;
  4323. }
  4324. write_lock(&map_tree->map_tree.lock);
  4325. ret = add_extent_mapping(&map_tree->map_tree, em);
  4326. write_unlock(&map_tree->map_tree.lock);
  4327. BUG_ON(ret); /* Tree corruption */
  4328. free_extent_map(em);
  4329. return 0;
  4330. }
  4331. static void fill_device_from_item(struct extent_buffer *leaf,
  4332. struct btrfs_dev_item *dev_item,
  4333. struct btrfs_device *device)
  4334. {
  4335. unsigned long ptr;
  4336. device->devid = btrfs_device_id(leaf, dev_item);
  4337. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  4338. device->total_bytes = device->disk_total_bytes;
  4339. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  4340. device->type = btrfs_device_type(leaf, dev_item);
  4341. device->io_align = btrfs_device_io_align(leaf, dev_item);
  4342. device->io_width = btrfs_device_io_width(leaf, dev_item);
  4343. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  4344. WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
  4345. device->is_tgtdev_for_dev_replace = 0;
  4346. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  4347. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  4348. }
  4349. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  4350. {
  4351. struct btrfs_fs_devices *fs_devices;
  4352. int ret;
  4353. BUG_ON(!mutex_is_locked(&uuid_mutex));
  4354. fs_devices = root->fs_info->fs_devices->seed;
  4355. while (fs_devices) {
  4356. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4357. ret = 0;
  4358. goto out;
  4359. }
  4360. fs_devices = fs_devices->seed;
  4361. }
  4362. fs_devices = find_fsid(fsid);
  4363. if (!fs_devices) {
  4364. ret = -ENOENT;
  4365. goto out;
  4366. }
  4367. fs_devices = clone_fs_devices(fs_devices);
  4368. if (IS_ERR(fs_devices)) {
  4369. ret = PTR_ERR(fs_devices);
  4370. goto out;
  4371. }
  4372. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  4373. root->fs_info->bdev_holder);
  4374. if (ret) {
  4375. free_fs_devices(fs_devices);
  4376. goto out;
  4377. }
  4378. if (!fs_devices->seeding) {
  4379. __btrfs_close_devices(fs_devices);
  4380. free_fs_devices(fs_devices);
  4381. ret = -EINVAL;
  4382. goto out;
  4383. }
  4384. fs_devices->seed = root->fs_info->fs_devices->seed;
  4385. root->fs_info->fs_devices->seed = fs_devices;
  4386. out:
  4387. return ret;
  4388. }
  4389. static int read_one_dev(struct btrfs_root *root,
  4390. struct extent_buffer *leaf,
  4391. struct btrfs_dev_item *dev_item)
  4392. {
  4393. struct btrfs_device *device;
  4394. u64 devid;
  4395. int ret;
  4396. u8 fs_uuid[BTRFS_UUID_SIZE];
  4397. u8 dev_uuid[BTRFS_UUID_SIZE];
  4398. devid = btrfs_device_id(leaf, dev_item);
  4399. read_extent_buffer(leaf, dev_uuid,
  4400. (unsigned long)btrfs_device_uuid(dev_item),
  4401. BTRFS_UUID_SIZE);
  4402. read_extent_buffer(leaf, fs_uuid,
  4403. (unsigned long)btrfs_device_fsid(dev_item),
  4404. BTRFS_UUID_SIZE);
  4405. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  4406. ret = open_seed_devices(root, fs_uuid);
  4407. if (ret && !btrfs_test_opt(root, DEGRADED))
  4408. return ret;
  4409. }
  4410. device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
  4411. if (!device || !device->bdev) {
  4412. if (!btrfs_test_opt(root, DEGRADED))
  4413. return -EIO;
  4414. if (!device) {
  4415. printk(KERN_WARNING "warning devid %llu missing\n",
  4416. (unsigned long long)devid);
  4417. device = add_missing_dev(root, devid, dev_uuid);
  4418. if (!device)
  4419. return -ENOMEM;
  4420. } else if (!device->missing) {
  4421. /*
  4422. * this happens when a device that was properly setup
  4423. * in the device info lists suddenly goes bad.
  4424. * device->bdev is NULL, and so we have to set
  4425. * device->missing to one here
  4426. */
  4427. root->fs_info->fs_devices->missing_devices++;
  4428. device->missing = 1;
  4429. }
  4430. }
  4431. if (device->fs_devices != root->fs_info->fs_devices) {
  4432. BUG_ON(device->writeable);
  4433. if (device->generation !=
  4434. btrfs_device_generation(leaf, dev_item))
  4435. return -EINVAL;
  4436. }
  4437. fill_device_from_item(leaf, dev_item, device);
  4438. device->dev_root = root->fs_info->dev_root;
  4439. device->in_fs_metadata = 1;
  4440. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  4441. device->fs_devices->total_rw_bytes += device->total_bytes;
  4442. spin_lock(&root->fs_info->free_chunk_lock);
  4443. root->fs_info->free_chunk_space += device->total_bytes -
  4444. device->bytes_used;
  4445. spin_unlock(&root->fs_info->free_chunk_lock);
  4446. }
  4447. ret = 0;
  4448. return ret;
  4449. }
  4450. int btrfs_read_sys_array(struct btrfs_root *root)
  4451. {
  4452. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  4453. struct extent_buffer *sb;
  4454. struct btrfs_disk_key *disk_key;
  4455. struct btrfs_chunk *chunk;
  4456. u8 *ptr;
  4457. unsigned long sb_ptr;
  4458. int ret = 0;
  4459. u32 num_stripes;
  4460. u32 array_size;
  4461. u32 len = 0;
  4462. u32 cur;
  4463. struct btrfs_key key;
  4464. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  4465. BTRFS_SUPER_INFO_SIZE);
  4466. if (!sb)
  4467. return -ENOMEM;
  4468. btrfs_set_buffer_uptodate(sb);
  4469. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  4470. /*
  4471. * The sb extent buffer is artifical and just used to read the system array.
  4472. * btrfs_set_buffer_uptodate() call does not properly mark all it's
  4473. * pages up-to-date when the page is larger: extent does not cover the
  4474. * whole page and consequently check_page_uptodate does not find all
  4475. * the page's extents up-to-date (the hole beyond sb),
  4476. * write_extent_buffer then triggers a WARN_ON.
  4477. *
  4478. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  4479. * but sb spans only this function. Add an explicit SetPageUptodate call
  4480. * to silence the warning eg. on PowerPC 64.
  4481. */
  4482. if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
  4483. SetPageUptodate(sb->pages[0]);
  4484. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  4485. array_size = btrfs_super_sys_array_size(super_copy);
  4486. ptr = super_copy->sys_chunk_array;
  4487. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  4488. cur = 0;
  4489. while (cur < array_size) {
  4490. disk_key = (struct btrfs_disk_key *)ptr;
  4491. btrfs_disk_key_to_cpu(&key, disk_key);
  4492. len = sizeof(*disk_key); ptr += len;
  4493. sb_ptr += len;
  4494. cur += len;
  4495. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  4496. chunk = (struct btrfs_chunk *)sb_ptr;
  4497. ret = read_one_chunk(root, &key, sb, chunk);
  4498. if (ret)
  4499. break;
  4500. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  4501. len = btrfs_chunk_item_size(num_stripes);
  4502. } else {
  4503. ret = -EIO;
  4504. break;
  4505. }
  4506. ptr += len;
  4507. sb_ptr += len;
  4508. cur += len;
  4509. }
  4510. free_extent_buffer(sb);
  4511. return ret;
  4512. }
  4513. int btrfs_read_chunk_tree(struct btrfs_root *root)
  4514. {
  4515. struct btrfs_path *path;
  4516. struct extent_buffer *leaf;
  4517. struct btrfs_key key;
  4518. struct btrfs_key found_key;
  4519. int ret;
  4520. int slot;
  4521. root = root->fs_info->chunk_root;
  4522. path = btrfs_alloc_path();
  4523. if (!path)
  4524. return -ENOMEM;
  4525. mutex_lock(&uuid_mutex);
  4526. lock_chunks(root);
  4527. /* first we search for all of the device items, and then we
  4528. * read in all of the chunk items. This way we can create chunk
  4529. * mappings that reference all of the devices that are afound
  4530. */
  4531. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  4532. key.offset = 0;
  4533. key.type = 0;
  4534. again:
  4535. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4536. if (ret < 0)
  4537. goto error;
  4538. while (1) {
  4539. leaf = path->nodes[0];
  4540. slot = path->slots[0];
  4541. if (slot >= btrfs_header_nritems(leaf)) {
  4542. ret = btrfs_next_leaf(root, path);
  4543. if (ret == 0)
  4544. continue;
  4545. if (ret < 0)
  4546. goto error;
  4547. break;
  4548. }
  4549. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4550. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4551. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  4552. break;
  4553. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  4554. struct btrfs_dev_item *dev_item;
  4555. dev_item = btrfs_item_ptr(leaf, slot,
  4556. struct btrfs_dev_item);
  4557. ret = read_one_dev(root, leaf, dev_item);
  4558. if (ret)
  4559. goto error;
  4560. }
  4561. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  4562. struct btrfs_chunk *chunk;
  4563. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  4564. ret = read_one_chunk(root, &found_key, leaf, chunk);
  4565. if (ret)
  4566. goto error;
  4567. }
  4568. path->slots[0]++;
  4569. }
  4570. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4571. key.objectid = 0;
  4572. btrfs_release_path(path);
  4573. goto again;
  4574. }
  4575. ret = 0;
  4576. error:
  4577. unlock_chunks(root);
  4578. mutex_unlock(&uuid_mutex);
  4579. btrfs_free_path(path);
  4580. return ret;
  4581. }
  4582. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  4583. {
  4584. int i;
  4585. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4586. btrfs_dev_stat_reset(dev, i);
  4587. }
  4588. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  4589. {
  4590. struct btrfs_key key;
  4591. struct btrfs_key found_key;
  4592. struct btrfs_root *dev_root = fs_info->dev_root;
  4593. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  4594. struct extent_buffer *eb;
  4595. int slot;
  4596. int ret = 0;
  4597. struct btrfs_device *device;
  4598. struct btrfs_path *path = NULL;
  4599. int i;
  4600. path = btrfs_alloc_path();
  4601. if (!path) {
  4602. ret = -ENOMEM;
  4603. goto out;
  4604. }
  4605. mutex_lock(&fs_devices->device_list_mutex);
  4606. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  4607. int item_size;
  4608. struct btrfs_dev_stats_item *ptr;
  4609. key.objectid = 0;
  4610. key.type = BTRFS_DEV_STATS_KEY;
  4611. key.offset = device->devid;
  4612. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  4613. if (ret) {
  4614. __btrfs_reset_dev_stats(device);
  4615. device->dev_stats_valid = 1;
  4616. btrfs_release_path(path);
  4617. continue;
  4618. }
  4619. slot = path->slots[0];
  4620. eb = path->nodes[0];
  4621. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4622. item_size = btrfs_item_size_nr(eb, slot);
  4623. ptr = btrfs_item_ptr(eb, slot,
  4624. struct btrfs_dev_stats_item);
  4625. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  4626. if (item_size >= (1 + i) * sizeof(__le64))
  4627. btrfs_dev_stat_set(device, i,
  4628. btrfs_dev_stats_value(eb, ptr, i));
  4629. else
  4630. btrfs_dev_stat_reset(device, i);
  4631. }
  4632. device->dev_stats_valid = 1;
  4633. btrfs_dev_stat_print_on_load(device);
  4634. btrfs_release_path(path);
  4635. }
  4636. mutex_unlock(&fs_devices->device_list_mutex);
  4637. out:
  4638. btrfs_free_path(path);
  4639. return ret < 0 ? ret : 0;
  4640. }
  4641. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  4642. struct btrfs_root *dev_root,
  4643. struct btrfs_device *device)
  4644. {
  4645. struct btrfs_path *path;
  4646. struct btrfs_key key;
  4647. struct extent_buffer *eb;
  4648. struct btrfs_dev_stats_item *ptr;
  4649. int ret;
  4650. int i;
  4651. key.objectid = 0;
  4652. key.type = BTRFS_DEV_STATS_KEY;
  4653. key.offset = device->devid;
  4654. path = btrfs_alloc_path();
  4655. BUG_ON(!path);
  4656. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  4657. if (ret < 0) {
  4658. printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
  4659. ret, rcu_str_deref(device->name));
  4660. goto out;
  4661. }
  4662. if (ret == 0 &&
  4663. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  4664. /* need to delete old one and insert a new one */
  4665. ret = btrfs_del_item(trans, dev_root, path);
  4666. if (ret != 0) {
  4667. printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
  4668. rcu_str_deref(device->name), ret);
  4669. goto out;
  4670. }
  4671. ret = 1;
  4672. }
  4673. if (ret == 1) {
  4674. /* need to insert a new item */
  4675. btrfs_release_path(path);
  4676. ret = btrfs_insert_empty_item(trans, dev_root, path,
  4677. &key, sizeof(*ptr));
  4678. if (ret < 0) {
  4679. printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
  4680. rcu_str_deref(device->name), ret);
  4681. goto out;
  4682. }
  4683. }
  4684. eb = path->nodes[0];
  4685. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  4686. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4687. btrfs_set_dev_stats_value(eb, ptr, i,
  4688. btrfs_dev_stat_read(device, i));
  4689. btrfs_mark_buffer_dirty(eb);
  4690. out:
  4691. btrfs_free_path(path);
  4692. return ret;
  4693. }
  4694. /*
  4695. * called from commit_transaction. Writes all changed device stats to disk.
  4696. */
  4697. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  4698. struct btrfs_fs_info *fs_info)
  4699. {
  4700. struct btrfs_root *dev_root = fs_info->dev_root;
  4701. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  4702. struct btrfs_device *device;
  4703. int ret = 0;
  4704. mutex_lock(&fs_devices->device_list_mutex);
  4705. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  4706. if (!device->dev_stats_valid || !device->dev_stats_dirty)
  4707. continue;
  4708. ret = update_dev_stat_item(trans, dev_root, device);
  4709. if (!ret)
  4710. device->dev_stats_dirty = 0;
  4711. }
  4712. mutex_unlock(&fs_devices->device_list_mutex);
  4713. return ret;
  4714. }
  4715. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  4716. {
  4717. btrfs_dev_stat_inc(dev, index);
  4718. btrfs_dev_stat_print_on_error(dev);
  4719. }
  4720. void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  4721. {
  4722. if (!dev->dev_stats_valid)
  4723. return;
  4724. printk_ratelimited_in_rcu(KERN_ERR
  4725. "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  4726. rcu_str_deref(dev->name),
  4727. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  4728. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  4729. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  4730. btrfs_dev_stat_read(dev,
  4731. BTRFS_DEV_STAT_CORRUPTION_ERRS),
  4732. btrfs_dev_stat_read(dev,
  4733. BTRFS_DEV_STAT_GENERATION_ERRS));
  4734. }
  4735. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  4736. {
  4737. int i;
  4738. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4739. if (btrfs_dev_stat_read(dev, i) != 0)
  4740. break;
  4741. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  4742. return; /* all values == 0, suppress message */
  4743. printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  4744. rcu_str_deref(dev->name),
  4745. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  4746. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  4747. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  4748. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  4749. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  4750. }
  4751. int btrfs_get_dev_stats(struct btrfs_root *root,
  4752. struct btrfs_ioctl_get_dev_stats *stats)
  4753. {
  4754. struct btrfs_device *dev;
  4755. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  4756. int i;
  4757. mutex_lock(&fs_devices->device_list_mutex);
  4758. dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
  4759. mutex_unlock(&fs_devices->device_list_mutex);
  4760. if (!dev) {
  4761. printk(KERN_WARNING
  4762. "btrfs: get dev_stats failed, device not found\n");
  4763. return -ENODEV;
  4764. } else if (!dev->dev_stats_valid) {
  4765. printk(KERN_WARNING
  4766. "btrfs: get dev_stats failed, not yet valid\n");
  4767. return -ENODEV;
  4768. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  4769. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  4770. if (stats->nr_items > i)
  4771. stats->values[i] =
  4772. btrfs_dev_stat_read_and_reset(dev, i);
  4773. else
  4774. btrfs_dev_stat_reset(dev, i);
  4775. }
  4776. } else {
  4777. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4778. if (stats->nr_items > i)
  4779. stats->values[i] = btrfs_dev_stat_read(dev, i);
  4780. }
  4781. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  4782. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  4783. return 0;
  4784. }
  4785. int btrfs_scratch_superblock(struct btrfs_device *device)
  4786. {
  4787. struct buffer_head *bh;
  4788. struct btrfs_super_block *disk_super;
  4789. bh = btrfs_read_dev_super(device->bdev);
  4790. if (!bh)
  4791. return -EINVAL;
  4792. disk_super = (struct btrfs_super_block *)bh->b_data;
  4793. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  4794. set_buffer_dirty(bh);
  4795. sync_dirty_buffer(bh);
  4796. brelse(bh);
  4797. return 0;
  4798. }