volumes.c 173 KB

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