volumes.c 111 KB

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