volumes.c 176 KB

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