volumes.c 160 KB

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