volumes.c 181 KB

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