volumes.c 131 KB

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