volumes.c 187 KB

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