volumes.c 128 KB

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