volumes.c 146 KB

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