volumes.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393
  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. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2125. if (ret < 0)
  2126. goto error;
  2127. /*
  2128. * this shouldn't happen, it means the last relocate
  2129. * failed
  2130. */
  2131. if (ret == 0)
  2132. BUG(); /* FIXME break ? */
  2133. ret = btrfs_previous_item(chunk_root, path, 0,
  2134. BTRFS_CHUNK_ITEM_KEY);
  2135. if (ret) {
  2136. ret = 0;
  2137. break;
  2138. }
  2139. leaf = path->nodes[0];
  2140. slot = path->slots[0];
  2141. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2142. if (found_key.objectid != key.objectid)
  2143. break;
  2144. /* chunk zero is special */
  2145. if (found_key.offset == 0)
  2146. break;
  2147. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2148. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2149. found_key.offset);
  2150. btrfs_release_path(path);
  2151. if (!ret)
  2152. goto loop;
  2153. ret = btrfs_relocate_chunk(chunk_root,
  2154. chunk_root->root_key.objectid,
  2155. found_key.objectid,
  2156. found_key.offset);
  2157. if (ret && ret != -ENOSPC)
  2158. goto error;
  2159. if (ret == -ENOSPC)
  2160. enospc_errors++;
  2161. loop:
  2162. key.offset = found_key.offset - 1;
  2163. }
  2164. error:
  2165. btrfs_free_path(path);
  2166. if (enospc_errors) {
  2167. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2168. enospc_errors);
  2169. if (!ret)
  2170. ret = -ENOSPC;
  2171. }
  2172. return ret;
  2173. }
  2174. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2175. {
  2176. int ret;
  2177. unset_balance_control(fs_info);
  2178. ret = del_balance_item(fs_info->tree_root);
  2179. BUG_ON(ret);
  2180. }
  2181. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  2182. struct btrfs_ioctl_balance_args *bargs);
  2183. /*
  2184. * Should be called with both balance and volume mutexes held
  2185. */
  2186. int btrfs_balance(struct btrfs_balance_control *bctl,
  2187. struct btrfs_ioctl_balance_args *bargs)
  2188. {
  2189. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2190. u64 allowed;
  2191. int ret;
  2192. if (btrfs_fs_closing(fs_info)) {
  2193. ret = -EINVAL;
  2194. goto out;
  2195. }
  2196. /*
  2197. * In case of mixed groups both data and meta should be picked,
  2198. * and identical options should be given for both of them.
  2199. */
  2200. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2201. if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2202. (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
  2203. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2204. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2205. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2206. printk(KERN_ERR "btrfs: with mixed groups data and "
  2207. "metadata balance options must be the same\n");
  2208. ret = -EINVAL;
  2209. goto out;
  2210. }
  2211. }
  2212. /*
  2213. * Profile changing sanity checks. Skip them if a simple
  2214. * balance is requested.
  2215. */
  2216. if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
  2217. BTRFS_BALANCE_ARGS_CONVERT))
  2218. goto do_balance;
  2219. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2220. if (fs_info->fs_devices->num_devices == 1)
  2221. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2222. else if (fs_info->fs_devices->num_devices < 4)
  2223. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2224. else
  2225. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2226. BTRFS_BLOCK_GROUP_RAID10);
  2227. if (!profile_is_valid(bctl->data.target, 1) ||
  2228. bctl->data.target & ~allowed) {
  2229. printk(KERN_ERR "btrfs: unable to start balance with target "
  2230. "data profile %llu\n",
  2231. (unsigned long long)bctl->data.target);
  2232. ret = -EINVAL;
  2233. goto out;
  2234. }
  2235. if (!profile_is_valid(bctl->meta.target, 1) ||
  2236. bctl->meta.target & ~allowed) {
  2237. printk(KERN_ERR "btrfs: unable to start balance with target "
  2238. "metadata profile %llu\n",
  2239. (unsigned long long)bctl->meta.target);
  2240. ret = -EINVAL;
  2241. goto out;
  2242. }
  2243. if (!profile_is_valid(bctl->sys.target, 1) ||
  2244. bctl->sys.target & ~allowed) {
  2245. printk(KERN_ERR "btrfs: unable to start balance with target "
  2246. "system profile %llu\n",
  2247. (unsigned long long)bctl->sys.target);
  2248. ret = -EINVAL;
  2249. goto out;
  2250. }
  2251. if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
  2252. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2253. ret = -EINVAL;
  2254. goto out;
  2255. }
  2256. /* allow to reduce meta or sys integrity only if force set */
  2257. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2258. BTRFS_BLOCK_GROUP_RAID10;
  2259. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2260. (fs_info->avail_system_alloc_bits & allowed) &&
  2261. !(bctl->sys.target & allowed)) ||
  2262. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2263. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2264. !(bctl->meta.target & allowed))) {
  2265. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2266. printk(KERN_INFO "btrfs: force reducing metadata "
  2267. "integrity\n");
  2268. } else {
  2269. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2270. "integrity, use force if you want this\n");
  2271. ret = -EINVAL;
  2272. goto out;
  2273. }
  2274. }
  2275. do_balance:
  2276. ret = insert_balance_item(fs_info->tree_root, bctl);
  2277. if (ret && ret != -EEXIST)
  2278. goto out;
  2279. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2280. BUG_ON(ret == -EEXIST);
  2281. set_balance_control(bctl);
  2282. } else {
  2283. BUG_ON(ret != -EEXIST);
  2284. spin_lock(&fs_info->balance_lock);
  2285. update_balance_args(bctl);
  2286. spin_unlock(&fs_info->balance_lock);
  2287. }
  2288. mutex_unlock(&fs_info->balance_mutex);
  2289. ret = __btrfs_balance(fs_info);
  2290. mutex_lock(&fs_info->balance_mutex);
  2291. if (bargs) {
  2292. memset(bargs, 0, sizeof(*bargs));
  2293. update_ioctl_balance_args(fs_info, bargs);
  2294. }
  2295. __cancel_balance(fs_info);
  2296. return ret;
  2297. out:
  2298. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2299. __cancel_balance(fs_info);
  2300. else
  2301. kfree(bctl);
  2302. return ret;
  2303. }
  2304. static int balance_kthread(void *data)
  2305. {
  2306. struct btrfs_balance_control *bctl =
  2307. (struct btrfs_balance_control *)data;
  2308. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2309. int ret;
  2310. mutex_lock(&fs_info->volume_mutex);
  2311. mutex_lock(&fs_info->balance_mutex);
  2312. set_balance_control(bctl);
  2313. printk(KERN_INFO "btrfs: continuing balance\n");
  2314. ret = btrfs_balance(bctl, NULL);
  2315. mutex_unlock(&fs_info->balance_mutex);
  2316. mutex_unlock(&fs_info->volume_mutex);
  2317. return ret;
  2318. }
  2319. int btrfs_recover_balance(struct btrfs_root *tree_root)
  2320. {
  2321. struct task_struct *tsk;
  2322. struct btrfs_balance_control *bctl;
  2323. struct btrfs_balance_item *item;
  2324. struct btrfs_disk_balance_args disk_bargs;
  2325. struct btrfs_path *path;
  2326. struct extent_buffer *leaf;
  2327. struct btrfs_key key;
  2328. int ret;
  2329. path = btrfs_alloc_path();
  2330. if (!path)
  2331. return -ENOMEM;
  2332. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2333. if (!bctl) {
  2334. ret = -ENOMEM;
  2335. goto out;
  2336. }
  2337. key.objectid = BTRFS_BALANCE_OBJECTID;
  2338. key.type = BTRFS_BALANCE_ITEM_KEY;
  2339. key.offset = 0;
  2340. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  2341. if (ret < 0)
  2342. goto out_bctl;
  2343. if (ret > 0) { /* ret = -ENOENT; */
  2344. ret = 0;
  2345. goto out_bctl;
  2346. }
  2347. leaf = path->nodes[0];
  2348. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2349. bctl->fs_info = tree_root->fs_info;
  2350. bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
  2351. btrfs_balance_data(leaf, item, &disk_bargs);
  2352. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2353. btrfs_balance_meta(leaf, item, &disk_bargs);
  2354. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2355. btrfs_balance_sys(leaf, item, &disk_bargs);
  2356. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2357. tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
  2358. if (IS_ERR(tsk))
  2359. ret = PTR_ERR(tsk);
  2360. else
  2361. goto out;
  2362. out_bctl:
  2363. kfree(bctl);
  2364. out:
  2365. btrfs_free_path(path);
  2366. return ret;
  2367. }
  2368. /*
  2369. * shrinking a device means finding all of the device extents past
  2370. * the new size, and then following the back refs to the chunks.
  2371. * The chunk relocation code actually frees the device extent
  2372. */
  2373. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2374. {
  2375. struct btrfs_trans_handle *trans;
  2376. struct btrfs_root *root = device->dev_root;
  2377. struct btrfs_dev_extent *dev_extent = NULL;
  2378. struct btrfs_path *path;
  2379. u64 length;
  2380. u64 chunk_tree;
  2381. u64 chunk_objectid;
  2382. u64 chunk_offset;
  2383. int ret;
  2384. int slot;
  2385. int failed = 0;
  2386. bool retried = false;
  2387. struct extent_buffer *l;
  2388. struct btrfs_key key;
  2389. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2390. u64 old_total = btrfs_super_total_bytes(super_copy);
  2391. u64 old_size = device->total_bytes;
  2392. u64 diff = device->total_bytes - new_size;
  2393. if (new_size >= device->total_bytes)
  2394. return -EINVAL;
  2395. path = btrfs_alloc_path();
  2396. if (!path)
  2397. return -ENOMEM;
  2398. path->reada = 2;
  2399. lock_chunks(root);
  2400. device->total_bytes = new_size;
  2401. if (device->writeable) {
  2402. device->fs_devices->total_rw_bytes -= diff;
  2403. spin_lock(&root->fs_info->free_chunk_lock);
  2404. root->fs_info->free_chunk_space -= diff;
  2405. spin_unlock(&root->fs_info->free_chunk_lock);
  2406. }
  2407. unlock_chunks(root);
  2408. again:
  2409. key.objectid = device->devid;
  2410. key.offset = (u64)-1;
  2411. key.type = BTRFS_DEV_EXTENT_KEY;
  2412. while (1) {
  2413. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2414. if (ret < 0)
  2415. goto done;
  2416. ret = btrfs_previous_item(root, path, 0, key.type);
  2417. if (ret < 0)
  2418. goto done;
  2419. if (ret) {
  2420. ret = 0;
  2421. btrfs_release_path(path);
  2422. break;
  2423. }
  2424. l = path->nodes[0];
  2425. slot = path->slots[0];
  2426. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2427. if (key.objectid != device->devid) {
  2428. btrfs_release_path(path);
  2429. break;
  2430. }
  2431. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2432. length = btrfs_dev_extent_length(l, dev_extent);
  2433. if (key.offset + length <= new_size) {
  2434. btrfs_release_path(path);
  2435. break;
  2436. }
  2437. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2438. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2439. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2440. btrfs_release_path(path);
  2441. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2442. chunk_offset);
  2443. if (ret && ret != -ENOSPC)
  2444. goto done;
  2445. if (ret == -ENOSPC)
  2446. failed++;
  2447. key.offset -= 1;
  2448. }
  2449. if (failed && !retried) {
  2450. failed = 0;
  2451. retried = true;
  2452. goto again;
  2453. } else if (failed && retried) {
  2454. ret = -ENOSPC;
  2455. lock_chunks(root);
  2456. device->total_bytes = old_size;
  2457. if (device->writeable)
  2458. device->fs_devices->total_rw_bytes += diff;
  2459. spin_lock(&root->fs_info->free_chunk_lock);
  2460. root->fs_info->free_chunk_space += diff;
  2461. spin_unlock(&root->fs_info->free_chunk_lock);
  2462. unlock_chunks(root);
  2463. goto done;
  2464. }
  2465. /* Shrinking succeeded, else we would be at "done". */
  2466. trans = btrfs_start_transaction(root, 0);
  2467. if (IS_ERR(trans)) {
  2468. ret = PTR_ERR(trans);
  2469. goto done;
  2470. }
  2471. lock_chunks(root);
  2472. device->disk_total_bytes = new_size;
  2473. /* Now btrfs_update_device() will change the on-disk size. */
  2474. ret = btrfs_update_device(trans, device);
  2475. if (ret) {
  2476. unlock_chunks(root);
  2477. btrfs_end_transaction(trans, root);
  2478. goto done;
  2479. }
  2480. WARN_ON(diff > old_total);
  2481. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  2482. unlock_chunks(root);
  2483. btrfs_end_transaction(trans, root);
  2484. done:
  2485. btrfs_free_path(path);
  2486. return ret;
  2487. }
  2488. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  2489. struct btrfs_root *root,
  2490. struct btrfs_key *key,
  2491. struct btrfs_chunk *chunk, int item_size)
  2492. {
  2493. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2494. struct btrfs_disk_key disk_key;
  2495. u32 array_size;
  2496. u8 *ptr;
  2497. array_size = btrfs_super_sys_array_size(super_copy);
  2498. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2499. return -EFBIG;
  2500. ptr = super_copy->sys_chunk_array + array_size;
  2501. btrfs_cpu_key_to_disk(&disk_key, key);
  2502. memcpy(ptr, &disk_key, sizeof(disk_key));
  2503. ptr += sizeof(disk_key);
  2504. memcpy(ptr, chunk, item_size);
  2505. item_size += sizeof(disk_key);
  2506. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2507. return 0;
  2508. }
  2509. /*
  2510. * sort the devices in descending order by max_avail, total_avail
  2511. */
  2512. static int btrfs_cmp_device_info(const void *a, const void *b)
  2513. {
  2514. const struct btrfs_device_info *di_a = a;
  2515. const struct btrfs_device_info *di_b = b;
  2516. if (di_a->max_avail > di_b->max_avail)
  2517. return -1;
  2518. if (di_a->max_avail < di_b->max_avail)
  2519. return 1;
  2520. if (di_a->total_avail > di_b->total_avail)
  2521. return -1;
  2522. if (di_a->total_avail < di_b->total_avail)
  2523. return 1;
  2524. return 0;
  2525. }
  2526. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2527. struct btrfs_root *extent_root,
  2528. struct map_lookup **map_ret,
  2529. u64 *num_bytes_out, u64 *stripe_size_out,
  2530. u64 start, u64 type)
  2531. {
  2532. struct btrfs_fs_info *info = extent_root->fs_info;
  2533. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2534. struct list_head *cur;
  2535. struct map_lookup *map = NULL;
  2536. struct extent_map_tree *em_tree;
  2537. struct extent_map *em;
  2538. struct btrfs_device_info *devices_info = NULL;
  2539. u64 total_avail;
  2540. int num_stripes; /* total number of stripes to allocate */
  2541. int sub_stripes; /* sub_stripes info for map */
  2542. int dev_stripes; /* stripes per dev */
  2543. int devs_max; /* max devs to use */
  2544. int devs_min; /* min devs needed */
  2545. int devs_increment; /* ndevs has to be a multiple of this */
  2546. int ncopies; /* how many copies to data has */
  2547. int ret;
  2548. u64 max_stripe_size;
  2549. u64 max_chunk_size;
  2550. u64 stripe_size;
  2551. u64 num_bytes;
  2552. int ndevs;
  2553. int i;
  2554. int j;
  2555. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2556. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2557. WARN_ON(1);
  2558. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2559. }
  2560. if (list_empty(&fs_devices->alloc_list))
  2561. return -ENOSPC;
  2562. sub_stripes = 1;
  2563. dev_stripes = 1;
  2564. devs_increment = 1;
  2565. ncopies = 1;
  2566. devs_max = 0; /* 0 == as many as possible */
  2567. devs_min = 1;
  2568. /*
  2569. * define the properties of each RAID type.
  2570. * FIXME: move this to a global table and use it in all RAID
  2571. * calculation code
  2572. */
  2573. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2574. dev_stripes = 2;
  2575. ncopies = 2;
  2576. devs_max = 1;
  2577. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2578. devs_min = 2;
  2579. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2580. devs_increment = 2;
  2581. ncopies = 2;
  2582. devs_max = 2;
  2583. devs_min = 2;
  2584. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2585. sub_stripes = 2;
  2586. devs_increment = 2;
  2587. ncopies = 2;
  2588. devs_min = 4;
  2589. } else {
  2590. devs_max = 1;
  2591. }
  2592. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2593. max_stripe_size = 1024 * 1024 * 1024;
  2594. max_chunk_size = 10 * max_stripe_size;
  2595. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2596. max_stripe_size = 256 * 1024 * 1024;
  2597. max_chunk_size = max_stripe_size;
  2598. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2599. max_stripe_size = 8 * 1024 * 1024;
  2600. max_chunk_size = 2 * max_stripe_size;
  2601. } else {
  2602. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2603. type);
  2604. BUG_ON(1);
  2605. }
  2606. /* we don't want a chunk larger than 10% of writeable space */
  2607. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2608. max_chunk_size);
  2609. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2610. GFP_NOFS);
  2611. if (!devices_info)
  2612. return -ENOMEM;
  2613. cur = fs_devices->alloc_list.next;
  2614. /*
  2615. * in the first pass through the devices list, we gather information
  2616. * about the available holes on each device.
  2617. */
  2618. ndevs = 0;
  2619. while (cur != &fs_devices->alloc_list) {
  2620. struct btrfs_device *device;
  2621. u64 max_avail;
  2622. u64 dev_offset;
  2623. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2624. cur = cur->next;
  2625. if (!device->writeable) {
  2626. printk(KERN_ERR
  2627. "btrfs: read-only device in alloc_list\n");
  2628. WARN_ON(1);
  2629. continue;
  2630. }
  2631. if (!device->in_fs_metadata)
  2632. continue;
  2633. if (device->total_bytes > device->bytes_used)
  2634. total_avail = device->total_bytes - device->bytes_used;
  2635. else
  2636. total_avail = 0;
  2637. /* If there is no space on this device, skip it. */
  2638. if (total_avail == 0)
  2639. continue;
  2640. ret = find_free_dev_extent(trans, device,
  2641. max_stripe_size * dev_stripes,
  2642. &dev_offset, &max_avail);
  2643. if (ret && ret != -ENOSPC)
  2644. goto error;
  2645. if (ret == 0)
  2646. max_avail = max_stripe_size * dev_stripes;
  2647. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2648. continue;
  2649. devices_info[ndevs].dev_offset = dev_offset;
  2650. devices_info[ndevs].max_avail = max_avail;
  2651. devices_info[ndevs].total_avail = total_avail;
  2652. devices_info[ndevs].dev = device;
  2653. ++ndevs;
  2654. }
  2655. /*
  2656. * now sort the devices by hole size / available space
  2657. */
  2658. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2659. btrfs_cmp_device_info, NULL);
  2660. /* round down to number of usable stripes */
  2661. ndevs -= ndevs % devs_increment;
  2662. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2663. ret = -ENOSPC;
  2664. goto error;
  2665. }
  2666. if (devs_max && ndevs > devs_max)
  2667. ndevs = devs_max;
  2668. /*
  2669. * the primary goal is to maximize the number of stripes, so use as many
  2670. * devices as possible, even if the stripes are not maximum sized.
  2671. */
  2672. stripe_size = devices_info[ndevs-1].max_avail;
  2673. num_stripes = ndevs * dev_stripes;
  2674. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2675. stripe_size = max_chunk_size * ncopies;
  2676. do_div(stripe_size, num_stripes);
  2677. }
  2678. do_div(stripe_size, dev_stripes);
  2679. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2680. stripe_size *= BTRFS_STRIPE_LEN;
  2681. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2682. if (!map) {
  2683. ret = -ENOMEM;
  2684. goto error;
  2685. }
  2686. map->num_stripes = num_stripes;
  2687. for (i = 0; i < ndevs; ++i) {
  2688. for (j = 0; j < dev_stripes; ++j) {
  2689. int s = i * dev_stripes + j;
  2690. map->stripes[s].dev = devices_info[i].dev;
  2691. map->stripes[s].physical = devices_info[i].dev_offset +
  2692. j * stripe_size;
  2693. }
  2694. }
  2695. map->sector_size = extent_root->sectorsize;
  2696. map->stripe_len = BTRFS_STRIPE_LEN;
  2697. map->io_align = BTRFS_STRIPE_LEN;
  2698. map->io_width = BTRFS_STRIPE_LEN;
  2699. map->type = type;
  2700. map->sub_stripes = sub_stripes;
  2701. *map_ret = map;
  2702. num_bytes = stripe_size * (num_stripes / ncopies);
  2703. *stripe_size_out = stripe_size;
  2704. *num_bytes_out = num_bytes;
  2705. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2706. em = alloc_extent_map();
  2707. if (!em) {
  2708. ret = -ENOMEM;
  2709. goto error;
  2710. }
  2711. em->bdev = (struct block_device *)map;
  2712. em->start = start;
  2713. em->len = num_bytes;
  2714. em->block_start = 0;
  2715. em->block_len = em->len;
  2716. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2717. write_lock(&em_tree->lock);
  2718. ret = add_extent_mapping(em_tree, em);
  2719. write_unlock(&em_tree->lock);
  2720. BUG_ON(ret);
  2721. free_extent_map(em);
  2722. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2723. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2724. start, num_bytes);
  2725. BUG_ON(ret);
  2726. for (i = 0; i < map->num_stripes; ++i) {
  2727. struct btrfs_device *device;
  2728. u64 dev_offset;
  2729. device = map->stripes[i].dev;
  2730. dev_offset = map->stripes[i].physical;
  2731. ret = btrfs_alloc_dev_extent(trans, device,
  2732. info->chunk_root->root_key.objectid,
  2733. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2734. start, dev_offset, stripe_size);
  2735. BUG_ON(ret);
  2736. }
  2737. kfree(devices_info);
  2738. return 0;
  2739. error:
  2740. kfree(map);
  2741. kfree(devices_info);
  2742. return ret;
  2743. }
  2744. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2745. struct btrfs_root *extent_root,
  2746. struct map_lookup *map, u64 chunk_offset,
  2747. u64 chunk_size, u64 stripe_size)
  2748. {
  2749. u64 dev_offset;
  2750. struct btrfs_key key;
  2751. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2752. struct btrfs_device *device;
  2753. struct btrfs_chunk *chunk;
  2754. struct btrfs_stripe *stripe;
  2755. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2756. int index = 0;
  2757. int ret;
  2758. chunk = kzalloc(item_size, GFP_NOFS);
  2759. if (!chunk)
  2760. return -ENOMEM;
  2761. index = 0;
  2762. while (index < map->num_stripes) {
  2763. device = map->stripes[index].dev;
  2764. device->bytes_used += stripe_size;
  2765. ret = btrfs_update_device(trans, device);
  2766. BUG_ON(ret);
  2767. index++;
  2768. }
  2769. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2770. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2771. map->num_stripes);
  2772. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2773. index = 0;
  2774. stripe = &chunk->stripe;
  2775. while (index < map->num_stripes) {
  2776. device = map->stripes[index].dev;
  2777. dev_offset = map->stripes[index].physical;
  2778. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2779. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2780. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2781. stripe++;
  2782. index++;
  2783. }
  2784. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2785. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2786. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2787. btrfs_set_stack_chunk_type(chunk, map->type);
  2788. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2789. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2790. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2791. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2792. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2793. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2794. key.type = BTRFS_CHUNK_ITEM_KEY;
  2795. key.offset = chunk_offset;
  2796. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2797. BUG_ON(ret);
  2798. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2799. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2800. item_size);
  2801. BUG_ON(ret);
  2802. }
  2803. kfree(chunk);
  2804. return 0;
  2805. }
  2806. /*
  2807. * Chunk allocation falls into two parts. The first part does works
  2808. * that make the new allocated chunk useable, but not do any operation
  2809. * that modifies the chunk tree. The second part does the works that
  2810. * require modifying the chunk tree. This division is important for the
  2811. * bootstrap process of adding storage to a seed btrfs.
  2812. */
  2813. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2814. struct btrfs_root *extent_root, u64 type)
  2815. {
  2816. u64 chunk_offset;
  2817. u64 chunk_size;
  2818. u64 stripe_size;
  2819. struct map_lookup *map;
  2820. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2821. int ret;
  2822. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2823. &chunk_offset);
  2824. if (ret)
  2825. return ret;
  2826. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2827. &stripe_size, chunk_offset, type);
  2828. if (ret)
  2829. return ret;
  2830. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2831. chunk_size, stripe_size);
  2832. BUG_ON(ret);
  2833. return 0;
  2834. }
  2835. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2836. struct btrfs_root *root,
  2837. struct btrfs_device *device)
  2838. {
  2839. u64 chunk_offset;
  2840. u64 sys_chunk_offset;
  2841. u64 chunk_size;
  2842. u64 sys_chunk_size;
  2843. u64 stripe_size;
  2844. u64 sys_stripe_size;
  2845. u64 alloc_profile;
  2846. struct map_lookup *map;
  2847. struct map_lookup *sys_map;
  2848. struct btrfs_fs_info *fs_info = root->fs_info;
  2849. struct btrfs_root *extent_root = fs_info->extent_root;
  2850. int ret;
  2851. ret = find_next_chunk(fs_info->chunk_root,
  2852. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2853. if (ret)
  2854. return ret;
  2855. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2856. fs_info->avail_metadata_alloc_bits;
  2857. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2858. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2859. &stripe_size, chunk_offset, alloc_profile);
  2860. BUG_ON(ret);
  2861. sys_chunk_offset = chunk_offset + chunk_size;
  2862. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2863. fs_info->avail_system_alloc_bits;
  2864. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2865. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2866. &sys_chunk_size, &sys_stripe_size,
  2867. sys_chunk_offset, alloc_profile);
  2868. BUG_ON(ret);
  2869. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2870. BUG_ON(ret);
  2871. /*
  2872. * Modifying chunk tree needs allocating new blocks from both
  2873. * system block group and metadata block group. So we only can
  2874. * do operations require modifying the chunk tree after both
  2875. * block groups were created.
  2876. */
  2877. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2878. chunk_size, stripe_size);
  2879. BUG_ON(ret);
  2880. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2881. sys_chunk_offset, sys_chunk_size,
  2882. sys_stripe_size);
  2883. BUG_ON(ret);
  2884. return 0;
  2885. }
  2886. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2887. {
  2888. struct extent_map *em;
  2889. struct map_lookup *map;
  2890. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2891. int readonly = 0;
  2892. int i;
  2893. read_lock(&map_tree->map_tree.lock);
  2894. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2895. read_unlock(&map_tree->map_tree.lock);
  2896. if (!em)
  2897. return 1;
  2898. if (btrfs_test_opt(root, DEGRADED)) {
  2899. free_extent_map(em);
  2900. return 0;
  2901. }
  2902. map = (struct map_lookup *)em->bdev;
  2903. for (i = 0; i < map->num_stripes; i++) {
  2904. if (!map->stripes[i].dev->writeable) {
  2905. readonly = 1;
  2906. break;
  2907. }
  2908. }
  2909. free_extent_map(em);
  2910. return readonly;
  2911. }
  2912. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2913. {
  2914. extent_map_tree_init(&tree->map_tree);
  2915. }
  2916. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2917. {
  2918. struct extent_map *em;
  2919. while (1) {
  2920. write_lock(&tree->map_tree.lock);
  2921. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2922. if (em)
  2923. remove_extent_mapping(&tree->map_tree, em);
  2924. write_unlock(&tree->map_tree.lock);
  2925. if (!em)
  2926. break;
  2927. kfree(em->bdev);
  2928. /* once for us */
  2929. free_extent_map(em);
  2930. /* once for the tree */
  2931. free_extent_map(em);
  2932. }
  2933. }
  2934. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2935. {
  2936. struct extent_map *em;
  2937. struct map_lookup *map;
  2938. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2939. int ret;
  2940. read_lock(&em_tree->lock);
  2941. em = lookup_extent_mapping(em_tree, logical, len);
  2942. read_unlock(&em_tree->lock);
  2943. BUG_ON(!em);
  2944. BUG_ON(em->start > logical || em->start + em->len < logical);
  2945. map = (struct map_lookup *)em->bdev;
  2946. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2947. ret = map->num_stripes;
  2948. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2949. ret = map->sub_stripes;
  2950. else
  2951. ret = 1;
  2952. free_extent_map(em);
  2953. return ret;
  2954. }
  2955. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2956. int optimal)
  2957. {
  2958. int i;
  2959. if (map->stripes[optimal].dev->bdev)
  2960. return optimal;
  2961. for (i = first; i < first + num; i++) {
  2962. if (map->stripes[i].dev->bdev)
  2963. return i;
  2964. }
  2965. /* we couldn't find one that doesn't fail. Just return something
  2966. * and the io error handling code will clean up eventually
  2967. */
  2968. return optimal;
  2969. }
  2970. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2971. u64 logical, u64 *length,
  2972. struct btrfs_bio **bbio_ret,
  2973. int mirror_num)
  2974. {
  2975. struct extent_map *em;
  2976. struct map_lookup *map;
  2977. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2978. u64 offset;
  2979. u64 stripe_offset;
  2980. u64 stripe_end_offset;
  2981. u64 stripe_nr;
  2982. u64 stripe_nr_orig;
  2983. u64 stripe_nr_end;
  2984. int stripes_allocated = 8;
  2985. int stripes_required = 1;
  2986. int stripe_index;
  2987. int i;
  2988. int num_stripes;
  2989. int max_errors = 0;
  2990. struct btrfs_bio *bbio = NULL;
  2991. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2992. stripes_allocated = 1;
  2993. again:
  2994. if (bbio_ret) {
  2995. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  2996. GFP_NOFS);
  2997. if (!bbio)
  2998. return -ENOMEM;
  2999. atomic_set(&bbio->error, 0);
  3000. }
  3001. read_lock(&em_tree->lock);
  3002. em = lookup_extent_mapping(em_tree, logical, *length);
  3003. read_unlock(&em_tree->lock);
  3004. if (!em) {
  3005. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  3006. (unsigned long long)logical,
  3007. (unsigned long long)*length);
  3008. BUG();
  3009. }
  3010. BUG_ON(em->start > logical || em->start + em->len < logical);
  3011. map = (struct map_lookup *)em->bdev;
  3012. offset = logical - em->start;
  3013. if (mirror_num > map->num_stripes)
  3014. mirror_num = 0;
  3015. /* if our btrfs_bio struct is too small, back off and try again */
  3016. if (rw & REQ_WRITE) {
  3017. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  3018. BTRFS_BLOCK_GROUP_DUP)) {
  3019. stripes_required = map->num_stripes;
  3020. max_errors = 1;
  3021. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3022. stripes_required = map->sub_stripes;
  3023. max_errors = 1;
  3024. }
  3025. }
  3026. if (rw & REQ_DISCARD) {
  3027. if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK)
  3028. stripes_required = map->num_stripes;
  3029. }
  3030. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  3031. stripes_allocated < stripes_required) {
  3032. stripes_allocated = map->num_stripes;
  3033. free_extent_map(em);
  3034. kfree(bbio);
  3035. goto again;
  3036. }
  3037. stripe_nr = offset;
  3038. /*
  3039. * stripe_nr counts the total number of stripes we have to stride
  3040. * to get to this block
  3041. */
  3042. do_div(stripe_nr, map->stripe_len);
  3043. stripe_offset = stripe_nr * map->stripe_len;
  3044. BUG_ON(offset < stripe_offset);
  3045. /* stripe_offset is the offset of this block in its stripe*/
  3046. stripe_offset = offset - stripe_offset;
  3047. if (rw & REQ_DISCARD)
  3048. *length = min_t(u64, em->len - offset, *length);
  3049. else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  3050. /* we limit the length of each bio to what fits in a stripe */
  3051. *length = min_t(u64, em->len - offset,
  3052. map->stripe_len - stripe_offset);
  3053. } else {
  3054. *length = em->len - offset;
  3055. }
  3056. if (!bbio_ret)
  3057. goto out;
  3058. num_stripes = 1;
  3059. stripe_index = 0;
  3060. stripe_nr_orig = stripe_nr;
  3061. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  3062. (~(map->stripe_len - 1));
  3063. do_div(stripe_nr_end, map->stripe_len);
  3064. stripe_end_offset = stripe_nr_end * map->stripe_len -
  3065. (offset + *length);
  3066. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3067. if (rw & REQ_DISCARD)
  3068. num_stripes = min_t(u64, map->num_stripes,
  3069. stripe_nr_end - stripe_nr_orig);
  3070. stripe_index = do_div(stripe_nr, map->num_stripes);
  3071. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  3072. if (rw & (REQ_WRITE | REQ_DISCARD))
  3073. num_stripes = map->num_stripes;
  3074. else if (mirror_num)
  3075. stripe_index = mirror_num - 1;
  3076. else {
  3077. stripe_index = find_live_mirror(map, 0,
  3078. map->num_stripes,
  3079. current->pid % map->num_stripes);
  3080. mirror_num = stripe_index + 1;
  3081. }
  3082. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  3083. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  3084. num_stripes = map->num_stripes;
  3085. } else if (mirror_num) {
  3086. stripe_index = mirror_num - 1;
  3087. } else {
  3088. mirror_num = 1;
  3089. }
  3090. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3091. int factor = map->num_stripes / map->sub_stripes;
  3092. stripe_index = do_div(stripe_nr, factor);
  3093. stripe_index *= map->sub_stripes;
  3094. if (rw & REQ_WRITE)
  3095. num_stripes = map->sub_stripes;
  3096. else if (rw & REQ_DISCARD)
  3097. num_stripes = min_t(u64, map->sub_stripes *
  3098. (stripe_nr_end - stripe_nr_orig),
  3099. map->num_stripes);
  3100. else if (mirror_num)
  3101. stripe_index += mirror_num - 1;
  3102. else {
  3103. stripe_index = find_live_mirror(map, stripe_index,
  3104. map->sub_stripes, stripe_index +
  3105. current->pid % map->sub_stripes);
  3106. mirror_num = stripe_index + 1;
  3107. }
  3108. } else {
  3109. /*
  3110. * after this do_div call, stripe_nr is the number of stripes
  3111. * on this device we have to walk to find the data, and
  3112. * stripe_index is the number of our device in the stripe array
  3113. */
  3114. stripe_index = do_div(stripe_nr, map->num_stripes);
  3115. mirror_num = stripe_index + 1;
  3116. }
  3117. BUG_ON(stripe_index >= map->num_stripes);
  3118. if (rw & REQ_DISCARD) {
  3119. for (i = 0; i < num_stripes; i++) {
  3120. bbio->stripes[i].physical =
  3121. map->stripes[stripe_index].physical +
  3122. stripe_offset + stripe_nr * map->stripe_len;
  3123. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  3124. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3125. u64 stripes;
  3126. u32 last_stripe = 0;
  3127. int j;
  3128. div_u64_rem(stripe_nr_end - 1,
  3129. map->num_stripes,
  3130. &last_stripe);
  3131. for (j = 0; j < map->num_stripes; j++) {
  3132. u32 test;
  3133. div_u64_rem(stripe_nr_end - 1 - j,
  3134. map->num_stripes, &test);
  3135. if (test == stripe_index)
  3136. break;
  3137. }
  3138. stripes = stripe_nr_end - 1 - j;
  3139. do_div(stripes, map->num_stripes);
  3140. bbio->stripes[i].length = map->stripe_len *
  3141. (stripes - stripe_nr + 1);
  3142. if (i == 0) {
  3143. bbio->stripes[i].length -=
  3144. stripe_offset;
  3145. stripe_offset = 0;
  3146. }
  3147. if (stripe_index == last_stripe)
  3148. bbio->stripes[i].length -=
  3149. stripe_end_offset;
  3150. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3151. u64 stripes;
  3152. int j;
  3153. int factor = map->num_stripes /
  3154. map->sub_stripes;
  3155. u32 last_stripe = 0;
  3156. div_u64_rem(stripe_nr_end - 1,
  3157. factor, &last_stripe);
  3158. last_stripe *= map->sub_stripes;
  3159. for (j = 0; j < factor; j++) {
  3160. u32 test;
  3161. div_u64_rem(stripe_nr_end - 1 - j,
  3162. factor, &test);
  3163. if (test ==
  3164. stripe_index / map->sub_stripes)
  3165. break;
  3166. }
  3167. stripes = stripe_nr_end - 1 - j;
  3168. do_div(stripes, factor);
  3169. bbio->stripes[i].length = map->stripe_len *
  3170. (stripes - stripe_nr + 1);
  3171. if (i < map->sub_stripes) {
  3172. bbio->stripes[i].length -=
  3173. stripe_offset;
  3174. if (i == map->sub_stripes - 1)
  3175. stripe_offset = 0;
  3176. }
  3177. if (stripe_index >= last_stripe &&
  3178. stripe_index <= (last_stripe +
  3179. map->sub_stripes - 1)) {
  3180. bbio->stripes[i].length -=
  3181. stripe_end_offset;
  3182. }
  3183. } else
  3184. bbio->stripes[i].length = *length;
  3185. stripe_index++;
  3186. if (stripe_index == map->num_stripes) {
  3187. /* This could only happen for RAID0/10 */
  3188. stripe_index = 0;
  3189. stripe_nr++;
  3190. }
  3191. }
  3192. } else {
  3193. for (i = 0; i < num_stripes; i++) {
  3194. bbio->stripes[i].physical =
  3195. map->stripes[stripe_index].physical +
  3196. stripe_offset +
  3197. stripe_nr * map->stripe_len;
  3198. bbio->stripes[i].dev =
  3199. map->stripes[stripe_index].dev;
  3200. stripe_index++;
  3201. }
  3202. }
  3203. if (bbio_ret) {
  3204. *bbio_ret = bbio;
  3205. bbio->num_stripes = num_stripes;
  3206. bbio->max_errors = max_errors;
  3207. bbio->mirror_num = mirror_num;
  3208. }
  3209. out:
  3210. free_extent_map(em);
  3211. return 0;
  3212. }
  3213. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  3214. u64 logical, u64 *length,
  3215. struct btrfs_bio **bbio_ret, int mirror_num)
  3216. {
  3217. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  3218. mirror_num);
  3219. }
  3220. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  3221. u64 chunk_start, u64 physical, u64 devid,
  3222. u64 **logical, int *naddrs, int *stripe_len)
  3223. {
  3224. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3225. struct extent_map *em;
  3226. struct map_lookup *map;
  3227. u64 *buf;
  3228. u64 bytenr;
  3229. u64 length;
  3230. u64 stripe_nr;
  3231. int i, j, nr = 0;
  3232. read_lock(&em_tree->lock);
  3233. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  3234. read_unlock(&em_tree->lock);
  3235. BUG_ON(!em || em->start != chunk_start);
  3236. map = (struct map_lookup *)em->bdev;
  3237. length = em->len;
  3238. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3239. do_div(length, map->num_stripes / map->sub_stripes);
  3240. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3241. do_div(length, map->num_stripes);
  3242. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  3243. BUG_ON(!buf);
  3244. for (i = 0; i < map->num_stripes; i++) {
  3245. if (devid && map->stripes[i].dev->devid != devid)
  3246. continue;
  3247. if (map->stripes[i].physical > physical ||
  3248. map->stripes[i].physical + length <= physical)
  3249. continue;
  3250. stripe_nr = physical - map->stripes[i].physical;
  3251. do_div(stripe_nr, map->stripe_len);
  3252. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3253. stripe_nr = stripe_nr * map->num_stripes + i;
  3254. do_div(stripe_nr, map->sub_stripes);
  3255. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3256. stripe_nr = stripe_nr * map->num_stripes + i;
  3257. }
  3258. bytenr = chunk_start + stripe_nr * map->stripe_len;
  3259. WARN_ON(nr >= map->num_stripes);
  3260. for (j = 0; j < nr; j++) {
  3261. if (buf[j] == bytenr)
  3262. break;
  3263. }
  3264. if (j == nr) {
  3265. WARN_ON(nr >= map->num_stripes);
  3266. buf[nr++] = bytenr;
  3267. }
  3268. }
  3269. *logical = buf;
  3270. *naddrs = nr;
  3271. *stripe_len = map->stripe_len;
  3272. free_extent_map(em);
  3273. return 0;
  3274. }
  3275. static void btrfs_end_bio(struct bio *bio, int err)
  3276. {
  3277. struct btrfs_bio *bbio = bio->bi_private;
  3278. int is_orig_bio = 0;
  3279. if (err)
  3280. atomic_inc(&bbio->error);
  3281. if (bio == bbio->orig_bio)
  3282. is_orig_bio = 1;
  3283. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3284. if (!is_orig_bio) {
  3285. bio_put(bio);
  3286. bio = bbio->orig_bio;
  3287. }
  3288. bio->bi_private = bbio->private;
  3289. bio->bi_end_io = bbio->end_io;
  3290. bio->bi_bdev = (struct block_device *)
  3291. (unsigned long)bbio->mirror_num;
  3292. /* only send an error to the higher layers if it is
  3293. * beyond the tolerance of the multi-bio
  3294. */
  3295. if (atomic_read(&bbio->error) > bbio->max_errors) {
  3296. err = -EIO;
  3297. } else {
  3298. /*
  3299. * this bio is actually up to date, we didn't
  3300. * go over the max number of errors
  3301. */
  3302. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3303. err = 0;
  3304. }
  3305. kfree(bbio);
  3306. bio_endio(bio, err);
  3307. } else if (!is_orig_bio) {
  3308. bio_put(bio);
  3309. }
  3310. }
  3311. struct async_sched {
  3312. struct bio *bio;
  3313. int rw;
  3314. struct btrfs_fs_info *info;
  3315. struct btrfs_work work;
  3316. };
  3317. /*
  3318. * see run_scheduled_bios for a description of why bios are collected for
  3319. * async submit.
  3320. *
  3321. * This will add one bio to the pending list for a device and make sure
  3322. * the work struct is scheduled.
  3323. */
  3324. static noinline int schedule_bio(struct btrfs_root *root,
  3325. struct btrfs_device *device,
  3326. int rw, struct bio *bio)
  3327. {
  3328. int should_queue = 1;
  3329. struct btrfs_pending_bios *pending_bios;
  3330. /* don't bother with additional async steps for reads, right now */
  3331. if (!(rw & REQ_WRITE)) {
  3332. bio_get(bio);
  3333. submit_bio(rw, bio);
  3334. bio_put(bio);
  3335. return 0;
  3336. }
  3337. /*
  3338. * nr_async_bios allows us to reliably return congestion to the
  3339. * higher layers. Otherwise, the async bio makes it appear we have
  3340. * made progress against dirty pages when we've really just put it
  3341. * on a queue for later
  3342. */
  3343. atomic_inc(&root->fs_info->nr_async_bios);
  3344. WARN_ON(bio->bi_next);
  3345. bio->bi_next = NULL;
  3346. bio->bi_rw |= rw;
  3347. spin_lock(&device->io_lock);
  3348. if (bio->bi_rw & REQ_SYNC)
  3349. pending_bios = &device->pending_sync_bios;
  3350. else
  3351. pending_bios = &device->pending_bios;
  3352. if (pending_bios->tail)
  3353. pending_bios->tail->bi_next = bio;
  3354. pending_bios->tail = bio;
  3355. if (!pending_bios->head)
  3356. pending_bios->head = bio;
  3357. if (device->running_pending)
  3358. should_queue = 0;
  3359. spin_unlock(&device->io_lock);
  3360. if (should_queue)
  3361. btrfs_queue_worker(&root->fs_info->submit_workers,
  3362. &device->work);
  3363. return 0;
  3364. }
  3365. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  3366. int mirror_num, int async_submit)
  3367. {
  3368. struct btrfs_mapping_tree *map_tree;
  3369. struct btrfs_device *dev;
  3370. struct bio *first_bio = bio;
  3371. u64 logical = (u64)bio->bi_sector << 9;
  3372. u64 length = 0;
  3373. u64 map_length;
  3374. int ret;
  3375. int dev_nr = 0;
  3376. int total_devs = 1;
  3377. struct btrfs_bio *bbio = NULL;
  3378. length = bio->bi_size;
  3379. map_tree = &root->fs_info->mapping_tree;
  3380. map_length = length;
  3381. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  3382. mirror_num);
  3383. BUG_ON(ret);
  3384. total_devs = bbio->num_stripes;
  3385. if (map_length < length) {
  3386. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  3387. "len %llu\n", (unsigned long long)logical,
  3388. (unsigned long long)length,
  3389. (unsigned long long)map_length);
  3390. BUG();
  3391. }
  3392. bbio->orig_bio = first_bio;
  3393. bbio->private = first_bio->bi_private;
  3394. bbio->end_io = first_bio->bi_end_io;
  3395. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  3396. while (dev_nr < total_devs) {
  3397. if (dev_nr < total_devs - 1) {
  3398. bio = bio_clone(first_bio, GFP_NOFS);
  3399. BUG_ON(!bio);
  3400. } else {
  3401. bio = first_bio;
  3402. }
  3403. bio->bi_private = bbio;
  3404. bio->bi_end_io = btrfs_end_bio;
  3405. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  3406. dev = bbio->stripes[dev_nr].dev;
  3407. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  3408. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  3409. "(%s id %llu), size=%u\n", rw,
  3410. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  3411. dev->name, dev->devid, bio->bi_size);
  3412. bio->bi_bdev = dev->bdev;
  3413. if (async_submit)
  3414. schedule_bio(root, dev, rw, bio);
  3415. else
  3416. submit_bio(rw, bio);
  3417. } else {
  3418. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  3419. bio->bi_sector = logical >> 9;
  3420. bio_endio(bio, -EIO);
  3421. }
  3422. dev_nr++;
  3423. }
  3424. return 0;
  3425. }
  3426. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  3427. u8 *uuid, u8 *fsid)
  3428. {
  3429. struct btrfs_device *device;
  3430. struct btrfs_fs_devices *cur_devices;
  3431. cur_devices = root->fs_info->fs_devices;
  3432. while (cur_devices) {
  3433. if (!fsid ||
  3434. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3435. device = __find_device(&cur_devices->devices,
  3436. devid, uuid);
  3437. if (device)
  3438. return device;
  3439. }
  3440. cur_devices = cur_devices->seed;
  3441. }
  3442. return NULL;
  3443. }
  3444. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  3445. u64 devid, u8 *dev_uuid)
  3446. {
  3447. struct btrfs_device *device;
  3448. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  3449. device = kzalloc(sizeof(*device), GFP_NOFS);
  3450. if (!device)
  3451. return NULL;
  3452. list_add(&device->dev_list,
  3453. &fs_devices->devices);
  3454. device->dev_root = root->fs_info->dev_root;
  3455. device->devid = devid;
  3456. device->work.func = pending_bios_fn;
  3457. device->fs_devices = fs_devices;
  3458. device->missing = 1;
  3459. fs_devices->num_devices++;
  3460. fs_devices->missing_devices++;
  3461. spin_lock_init(&device->io_lock);
  3462. INIT_LIST_HEAD(&device->dev_alloc_list);
  3463. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  3464. return device;
  3465. }
  3466. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  3467. struct extent_buffer *leaf,
  3468. struct btrfs_chunk *chunk)
  3469. {
  3470. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3471. struct map_lookup *map;
  3472. struct extent_map *em;
  3473. u64 logical;
  3474. u64 length;
  3475. u64 devid;
  3476. u8 uuid[BTRFS_UUID_SIZE];
  3477. int num_stripes;
  3478. int ret;
  3479. int i;
  3480. logical = key->offset;
  3481. length = btrfs_chunk_length(leaf, chunk);
  3482. read_lock(&map_tree->map_tree.lock);
  3483. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  3484. read_unlock(&map_tree->map_tree.lock);
  3485. /* already mapped? */
  3486. if (em && em->start <= logical && em->start + em->len > logical) {
  3487. free_extent_map(em);
  3488. return 0;
  3489. } else if (em) {
  3490. free_extent_map(em);
  3491. }
  3492. em = alloc_extent_map();
  3493. if (!em)
  3494. return -ENOMEM;
  3495. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3496. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3497. if (!map) {
  3498. free_extent_map(em);
  3499. return -ENOMEM;
  3500. }
  3501. em->bdev = (struct block_device *)map;
  3502. em->start = logical;
  3503. em->len = length;
  3504. em->block_start = 0;
  3505. em->block_len = em->len;
  3506. map->num_stripes = num_stripes;
  3507. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3508. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3509. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3510. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3511. map->type = btrfs_chunk_type(leaf, chunk);
  3512. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3513. for (i = 0; i < num_stripes; i++) {
  3514. map->stripes[i].physical =
  3515. btrfs_stripe_offset_nr(leaf, chunk, i);
  3516. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3517. read_extent_buffer(leaf, uuid, (unsigned long)
  3518. btrfs_stripe_dev_uuid_nr(chunk, i),
  3519. BTRFS_UUID_SIZE);
  3520. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3521. NULL);
  3522. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3523. kfree(map);
  3524. free_extent_map(em);
  3525. return -EIO;
  3526. }
  3527. if (!map->stripes[i].dev) {
  3528. map->stripes[i].dev =
  3529. add_missing_dev(root, devid, uuid);
  3530. if (!map->stripes[i].dev) {
  3531. kfree(map);
  3532. free_extent_map(em);
  3533. return -EIO;
  3534. }
  3535. }
  3536. map->stripes[i].dev->in_fs_metadata = 1;
  3537. }
  3538. write_lock(&map_tree->map_tree.lock);
  3539. ret = add_extent_mapping(&map_tree->map_tree, em);
  3540. write_unlock(&map_tree->map_tree.lock);
  3541. BUG_ON(ret);
  3542. free_extent_map(em);
  3543. return 0;
  3544. }
  3545. static int fill_device_from_item(struct extent_buffer *leaf,
  3546. struct btrfs_dev_item *dev_item,
  3547. struct btrfs_device *device)
  3548. {
  3549. unsigned long ptr;
  3550. device->devid = btrfs_device_id(leaf, dev_item);
  3551. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3552. device->total_bytes = device->disk_total_bytes;
  3553. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3554. device->type = btrfs_device_type(leaf, dev_item);
  3555. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3556. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3557. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3558. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3559. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3560. return 0;
  3561. }
  3562. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3563. {
  3564. struct btrfs_fs_devices *fs_devices;
  3565. int ret;
  3566. mutex_lock(&uuid_mutex);
  3567. fs_devices = root->fs_info->fs_devices->seed;
  3568. while (fs_devices) {
  3569. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3570. ret = 0;
  3571. goto out;
  3572. }
  3573. fs_devices = fs_devices->seed;
  3574. }
  3575. fs_devices = find_fsid(fsid);
  3576. if (!fs_devices) {
  3577. ret = -ENOENT;
  3578. goto out;
  3579. }
  3580. fs_devices = clone_fs_devices(fs_devices);
  3581. if (IS_ERR(fs_devices)) {
  3582. ret = PTR_ERR(fs_devices);
  3583. goto out;
  3584. }
  3585. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3586. root->fs_info->bdev_holder);
  3587. if (ret)
  3588. goto out;
  3589. if (!fs_devices->seeding) {
  3590. __btrfs_close_devices(fs_devices);
  3591. free_fs_devices(fs_devices);
  3592. ret = -EINVAL;
  3593. goto out;
  3594. }
  3595. fs_devices->seed = root->fs_info->fs_devices->seed;
  3596. root->fs_info->fs_devices->seed = fs_devices;
  3597. out:
  3598. mutex_unlock(&uuid_mutex);
  3599. return ret;
  3600. }
  3601. static int read_one_dev(struct btrfs_root *root,
  3602. struct extent_buffer *leaf,
  3603. struct btrfs_dev_item *dev_item)
  3604. {
  3605. struct btrfs_device *device;
  3606. u64 devid;
  3607. int ret;
  3608. u8 fs_uuid[BTRFS_UUID_SIZE];
  3609. u8 dev_uuid[BTRFS_UUID_SIZE];
  3610. devid = btrfs_device_id(leaf, dev_item);
  3611. read_extent_buffer(leaf, dev_uuid,
  3612. (unsigned long)btrfs_device_uuid(dev_item),
  3613. BTRFS_UUID_SIZE);
  3614. read_extent_buffer(leaf, fs_uuid,
  3615. (unsigned long)btrfs_device_fsid(dev_item),
  3616. BTRFS_UUID_SIZE);
  3617. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3618. ret = open_seed_devices(root, fs_uuid);
  3619. if (ret && !btrfs_test_opt(root, DEGRADED))
  3620. return ret;
  3621. }
  3622. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3623. if (!device || !device->bdev) {
  3624. if (!btrfs_test_opt(root, DEGRADED))
  3625. return -EIO;
  3626. if (!device) {
  3627. printk(KERN_WARNING "warning devid %llu missing\n",
  3628. (unsigned long long)devid);
  3629. device = add_missing_dev(root, devid, dev_uuid);
  3630. if (!device)
  3631. return -ENOMEM;
  3632. } else if (!device->missing) {
  3633. /*
  3634. * this happens when a device that was properly setup
  3635. * in the device info lists suddenly goes bad.
  3636. * device->bdev is NULL, and so we have to set
  3637. * device->missing to one here
  3638. */
  3639. root->fs_info->fs_devices->missing_devices++;
  3640. device->missing = 1;
  3641. }
  3642. }
  3643. if (device->fs_devices != root->fs_info->fs_devices) {
  3644. BUG_ON(device->writeable);
  3645. if (device->generation !=
  3646. btrfs_device_generation(leaf, dev_item))
  3647. return -EINVAL;
  3648. }
  3649. fill_device_from_item(leaf, dev_item, device);
  3650. device->dev_root = root->fs_info->dev_root;
  3651. device->in_fs_metadata = 1;
  3652. if (device->writeable) {
  3653. device->fs_devices->total_rw_bytes += device->total_bytes;
  3654. spin_lock(&root->fs_info->free_chunk_lock);
  3655. root->fs_info->free_chunk_space += device->total_bytes -
  3656. device->bytes_used;
  3657. spin_unlock(&root->fs_info->free_chunk_lock);
  3658. }
  3659. ret = 0;
  3660. return ret;
  3661. }
  3662. int btrfs_read_sys_array(struct btrfs_root *root)
  3663. {
  3664. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3665. struct extent_buffer *sb;
  3666. struct btrfs_disk_key *disk_key;
  3667. struct btrfs_chunk *chunk;
  3668. u8 *ptr;
  3669. unsigned long sb_ptr;
  3670. int ret = 0;
  3671. u32 num_stripes;
  3672. u32 array_size;
  3673. u32 len = 0;
  3674. u32 cur;
  3675. struct btrfs_key key;
  3676. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3677. BTRFS_SUPER_INFO_SIZE);
  3678. if (!sb)
  3679. return -ENOMEM;
  3680. btrfs_set_buffer_uptodate(sb);
  3681. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3682. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3683. array_size = btrfs_super_sys_array_size(super_copy);
  3684. ptr = super_copy->sys_chunk_array;
  3685. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3686. cur = 0;
  3687. while (cur < array_size) {
  3688. disk_key = (struct btrfs_disk_key *)ptr;
  3689. btrfs_disk_key_to_cpu(&key, disk_key);
  3690. len = sizeof(*disk_key); ptr += len;
  3691. sb_ptr += len;
  3692. cur += len;
  3693. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3694. chunk = (struct btrfs_chunk *)sb_ptr;
  3695. ret = read_one_chunk(root, &key, sb, chunk);
  3696. if (ret)
  3697. break;
  3698. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3699. len = btrfs_chunk_item_size(num_stripes);
  3700. } else {
  3701. ret = -EIO;
  3702. break;
  3703. }
  3704. ptr += len;
  3705. sb_ptr += len;
  3706. cur += len;
  3707. }
  3708. free_extent_buffer(sb);
  3709. return ret;
  3710. }
  3711. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3712. {
  3713. struct btrfs_path *path;
  3714. struct extent_buffer *leaf;
  3715. struct btrfs_key key;
  3716. struct btrfs_key found_key;
  3717. int ret;
  3718. int slot;
  3719. root = root->fs_info->chunk_root;
  3720. path = btrfs_alloc_path();
  3721. if (!path)
  3722. return -ENOMEM;
  3723. /* first we search for all of the device items, and then we
  3724. * read in all of the chunk items. This way we can create chunk
  3725. * mappings that reference all of the devices that are afound
  3726. */
  3727. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3728. key.offset = 0;
  3729. key.type = 0;
  3730. again:
  3731. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3732. if (ret < 0)
  3733. goto error;
  3734. while (1) {
  3735. leaf = path->nodes[0];
  3736. slot = path->slots[0];
  3737. if (slot >= btrfs_header_nritems(leaf)) {
  3738. ret = btrfs_next_leaf(root, path);
  3739. if (ret == 0)
  3740. continue;
  3741. if (ret < 0)
  3742. goto error;
  3743. break;
  3744. }
  3745. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3746. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3747. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3748. break;
  3749. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3750. struct btrfs_dev_item *dev_item;
  3751. dev_item = btrfs_item_ptr(leaf, slot,
  3752. struct btrfs_dev_item);
  3753. ret = read_one_dev(root, leaf, dev_item);
  3754. if (ret)
  3755. goto error;
  3756. }
  3757. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3758. struct btrfs_chunk *chunk;
  3759. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3760. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3761. if (ret)
  3762. goto error;
  3763. }
  3764. path->slots[0]++;
  3765. }
  3766. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3767. key.objectid = 0;
  3768. btrfs_release_path(path);
  3769. goto again;
  3770. }
  3771. ret = 0;
  3772. error:
  3773. btrfs_free_path(path);
  3774. return ret;
  3775. }