volumes.c 125 KB

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