volumes.c 160 KB

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