volumes.c 154 KB

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