volumes.c 126 KB

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