volumes.c 146 KB

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