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