volumes.c 177 KB

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