volumes.c 124 KB

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