volumes.c 163 KB

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