volumes.c 188 KB

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