volumes.c 166 KB

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