volumes.c 123 KB

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