volumes.c 143 KB

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