volumes.c 160 KB

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