volumes.c 160 KB

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