volumes.c 185 KB

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