volumes.c 187 KB

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