volumes.c 181 KB

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