volumes.c 173 KB

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