volumes.c 187 KB

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