volumes.c 182 KB

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