volumes.c 171 KB

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