volumes.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805
  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 <asm/div64.h>
  27. #include "compat.h"
  28. #include "ctree.h"
  29. #include "extent_map.h"
  30. #include "disk-io.h"
  31. #include "transaction.h"
  32. #include "print-tree.h"
  33. #include "volumes.h"
  34. #include "async-thread.h"
  35. #include "check-integrity.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. btrfsic_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. mutex_lock(&root->fs_info->volume_mutex);
  1110. all_avail = root->fs_info->avail_data_alloc_bits |
  1111. root->fs_info->avail_system_alloc_bits |
  1112. root->fs_info->avail_metadata_alloc_bits;
  1113. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  1114. root->fs_info->fs_devices->num_devices <= 4) {
  1115. printk(KERN_ERR "btrfs: unable to go below four devices "
  1116. "on raid10\n");
  1117. ret = -EINVAL;
  1118. goto out;
  1119. }
  1120. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  1121. root->fs_info->fs_devices->num_devices <= 2) {
  1122. printk(KERN_ERR "btrfs: unable to go below two "
  1123. "devices on raid1\n");
  1124. ret = -EINVAL;
  1125. goto out;
  1126. }
  1127. if (strcmp(device_path, "missing") == 0) {
  1128. struct list_head *devices;
  1129. struct btrfs_device *tmp;
  1130. device = NULL;
  1131. devices = &root->fs_info->fs_devices->devices;
  1132. /*
  1133. * It is safe to read the devices since the volume_mutex
  1134. * is held.
  1135. */
  1136. list_for_each_entry(tmp, devices, dev_list) {
  1137. if (tmp->in_fs_metadata && !tmp->bdev) {
  1138. device = tmp;
  1139. break;
  1140. }
  1141. }
  1142. bdev = NULL;
  1143. bh = NULL;
  1144. disk_super = NULL;
  1145. if (!device) {
  1146. printk(KERN_ERR "btrfs: no missing devices found to "
  1147. "remove\n");
  1148. goto out;
  1149. }
  1150. } else {
  1151. bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
  1152. root->fs_info->bdev_holder);
  1153. if (IS_ERR(bdev)) {
  1154. ret = PTR_ERR(bdev);
  1155. goto out;
  1156. }
  1157. set_blocksize(bdev, 4096);
  1158. bh = btrfs_read_dev_super(bdev);
  1159. if (!bh) {
  1160. ret = -EINVAL;
  1161. goto error_close;
  1162. }
  1163. disk_super = (struct btrfs_super_block *)bh->b_data;
  1164. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1165. dev_uuid = disk_super->dev_item.uuid;
  1166. device = btrfs_find_device(root, devid, dev_uuid,
  1167. disk_super->fsid);
  1168. if (!device) {
  1169. ret = -ENOENT;
  1170. goto error_brelse;
  1171. }
  1172. }
  1173. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1174. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1175. "device\n");
  1176. ret = -EINVAL;
  1177. goto error_brelse;
  1178. }
  1179. if (device->writeable) {
  1180. lock_chunks(root);
  1181. list_del_init(&device->dev_alloc_list);
  1182. unlock_chunks(root);
  1183. root->fs_info->fs_devices->rw_devices--;
  1184. clear_super = true;
  1185. }
  1186. ret = btrfs_shrink_device(device, 0);
  1187. if (ret)
  1188. goto error_undo;
  1189. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1190. if (ret)
  1191. goto error_undo;
  1192. spin_lock(&root->fs_info->free_chunk_lock);
  1193. root->fs_info->free_chunk_space = device->total_bytes -
  1194. device->bytes_used;
  1195. spin_unlock(&root->fs_info->free_chunk_lock);
  1196. device->in_fs_metadata = 0;
  1197. btrfs_scrub_cancel_dev(root, device);
  1198. /*
  1199. * the device list mutex makes sure that we don't change
  1200. * the device list while someone else is writing out all
  1201. * the device supers.
  1202. */
  1203. cur_devices = device->fs_devices;
  1204. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1205. list_del_rcu(&device->dev_list);
  1206. device->fs_devices->num_devices--;
  1207. if (device->missing)
  1208. root->fs_info->fs_devices->missing_devices--;
  1209. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1210. struct btrfs_device, dev_list);
  1211. if (device->bdev == root->fs_info->sb->s_bdev)
  1212. root->fs_info->sb->s_bdev = next_device->bdev;
  1213. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1214. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1215. if (device->bdev)
  1216. device->fs_devices->open_devices--;
  1217. call_rcu(&device->rcu, free_device);
  1218. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1219. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1220. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1221. if (cur_devices->open_devices == 0) {
  1222. struct btrfs_fs_devices *fs_devices;
  1223. fs_devices = root->fs_info->fs_devices;
  1224. while (fs_devices) {
  1225. if (fs_devices->seed == cur_devices)
  1226. break;
  1227. fs_devices = fs_devices->seed;
  1228. }
  1229. fs_devices->seed = cur_devices->seed;
  1230. cur_devices->seed = NULL;
  1231. lock_chunks(root);
  1232. __btrfs_close_devices(cur_devices);
  1233. unlock_chunks(root);
  1234. free_fs_devices(cur_devices);
  1235. }
  1236. /*
  1237. * at this point, the device is zero sized. We want to
  1238. * remove it from the devices list and zero out the old super
  1239. */
  1240. if (clear_super) {
  1241. /* make sure this device isn't detected as part of
  1242. * the FS anymore
  1243. */
  1244. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1245. set_buffer_dirty(bh);
  1246. sync_dirty_buffer(bh);
  1247. }
  1248. ret = 0;
  1249. error_brelse:
  1250. brelse(bh);
  1251. error_close:
  1252. if (bdev)
  1253. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1254. out:
  1255. mutex_unlock(&root->fs_info->volume_mutex);
  1256. mutex_unlock(&uuid_mutex);
  1257. return ret;
  1258. error_undo:
  1259. if (device->writeable) {
  1260. lock_chunks(root);
  1261. list_add(&device->dev_alloc_list,
  1262. &root->fs_info->fs_devices->alloc_list);
  1263. unlock_chunks(root);
  1264. root->fs_info->fs_devices->rw_devices++;
  1265. }
  1266. goto error_brelse;
  1267. }
  1268. /*
  1269. * does all the dirty work required for changing file system's UUID.
  1270. */
  1271. static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
  1272. struct btrfs_root *root)
  1273. {
  1274. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1275. struct btrfs_fs_devices *old_devices;
  1276. struct btrfs_fs_devices *seed_devices;
  1277. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1278. struct btrfs_device *device;
  1279. u64 super_flags;
  1280. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1281. if (!fs_devices->seeding)
  1282. return -EINVAL;
  1283. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1284. if (!seed_devices)
  1285. return -ENOMEM;
  1286. old_devices = clone_fs_devices(fs_devices);
  1287. if (IS_ERR(old_devices)) {
  1288. kfree(seed_devices);
  1289. return PTR_ERR(old_devices);
  1290. }
  1291. list_add(&old_devices->list, &fs_uuids);
  1292. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1293. seed_devices->opened = 1;
  1294. INIT_LIST_HEAD(&seed_devices->devices);
  1295. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1296. mutex_init(&seed_devices->device_list_mutex);
  1297. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1298. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1299. synchronize_rcu);
  1300. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1301. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1302. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1303. device->fs_devices = seed_devices;
  1304. }
  1305. fs_devices->seeding = 0;
  1306. fs_devices->num_devices = 0;
  1307. fs_devices->open_devices = 0;
  1308. fs_devices->seed = seed_devices;
  1309. generate_random_uuid(fs_devices->fsid);
  1310. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1311. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1312. super_flags = btrfs_super_flags(disk_super) &
  1313. ~BTRFS_SUPER_FLAG_SEEDING;
  1314. btrfs_set_super_flags(disk_super, super_flags);
  1315. return 0;
  1316. }
  1317. /*
  1318. * strore the expected generation for seed devices in device items.
  1319. */
  1320. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1321. struct btrfs_root *root)
  1322. {
  1323. struct btrfs_path *path;
  1324. struct extent_buffer *leaf;
  1325. struct btrfs_dev_item *dev_item;
  1326. struct btrfs_device *device;
  1327. struct btrfs_key key;
  1328. u8 fs_uuid[BTRFS_UUID_SIZE];
  1329. u8 dev_uuid[BTRFS_UUID_SIZE];
  1330. u64 devid;
  1331. int ret;
  1332. path = btrfs_alloc_path();
  1333. if (!path)
  1334. return -ENOMEM;
  1335. root = root->fs_info->chunk_root;
  1336. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1337. key.offset = 0;
  1338. key.type = BTRFS_DEV_ITEM_KEY;
  1339. while (1) {
  1340. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1341. if (ret < 0)
  1342. goto error;
  1343. leaf = path->nodes[0];
  1344. next_slot:
  1345. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1346. ret = btrfs_next_leaf(root, path);
  1347. if (ret > 0)
  1348. break;
  1349. if (ret < 0)
  1350. goto error;
  1351. leaf = path->nodes[0];
  1352. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1353. btrfs_release_path(path);
  1354. continue;
  1355. }
  1356. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1357. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1358. key.type != BTRFS_DEV_ITEM_KEY)
  1359. break;
  1360. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1361. struct btrfs_dev_item);
  1362. devid = btrfs_device_id(leaf, dev_item);
  1363. read_extent_buffer(leaf, dev_uuid,
  1364. (unsigned long)btrfs_device_uuid(dev_item),
  1365. BTRFS_UUID_SIZE);
  1366. read_extent_buffer(leaf, fs_uuid,
  1367. (unsigned long)btrfs_device_fsid(dev_item),
  1368. BTRFS_UUID_SIZE);
  1369. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1370. BUG_ON(!device);
  1371. if (device->fs_devices->seeding) {
  1372. btrfs_set_device_generation(leaf, dev_item,
  1373. device->generation);
  1374. btrfs_mark_buffer_dirty(leaf);
  1375. }
  1376. path->slots[0]++;
  1377. goto next_slot;
  1378. }
  1379. ret = 0;
  1380. error:
  1381. btrfs_free_path(path);
  1382. return ret;
  1383. }
  1384. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1385. {
  1386. struct request_queue *q;
  1387. struct btrfs_trans_handle *trans;
  1388. struct btrfs_device *device;
  1389. struct block_device *bdev;
  1390. struct list_head *devices;
  1391. struct super_block *sb = root->fs_info->sb;
  1392. u64 total_bytes;
  1393. int seeding_dev = 0;
  1394. int ret = 0;
  1395. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1396. return -EINVAL;
  1397. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1398. root->fs_info->bdev_holder);
  1399. if (IS_ERR(bdev))
  1400. return PTR_ERR(bdev);
  1401. if (root->fs_info->fs_devices->seeding) {
  1402. seeding_dev = 1;
  1403. down_write(&sb->s_umount);
  1404. mutex_lock(&uuid_mutex);
  1405. }
  1406. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1407. mutex_lock(&root->fs_info->volume_mutex);
  1408. devices = &root->fs_info->fs_devices->devices;
  1409. /*
  1410. * we have the volume lock, so we don't need the extra
  1411. * device list mutex while reading the list here.
  1412. */
  1413. list_for_each_entry(device, devices, dev_list) {
  1414. if (device->bdev == bdev) {
  1415. ret = -EEXIST;
  1416. goto error;
  1417. }
  1418. }
  1419. device = kzalloc(sizeof(*device), GFP_NOFS);
  1420. if (!device) {
  1421. /* we can safely leave the fs_devices entry around */
  1422. ret = -ENOMEM;
  1423. goto error;
  1424. }
  1425. device->name = kstrdup(device_path, GFP_NOFS);
  1426. if (!device->name) {
  1427. kfree(device);
  1428. ret = -ENOMEM;
  1429. goto error;
  1430. }
  1431. ret = find_next_devid(root, &device->devid);
  1432. if (ret) {
  1433. kfree(device->name);
  1434. kfree(device);
  1435. goto error;
  1436. }
  1437. trans = btrfs_start_transaction(root, 0);
  1438. if (IS_ERR(trans)) {
  1439. kfree(device->name);
  1440. kfree(device);
  1441. ret = PTR_ERR(trans);
  1442. goto error;
  1443. }
  1444. lock_chunks(root);
  1445. q = bdev_get_queue(bdev);
  1446. if (blk_queue_discard(q))
  1447. device->can_discard = 1;
  1448. device->writeable = 1;
  1449. device->work.func = pending_bios_fn;
  1450. generate_random_uuid(device->uuid);
  1451. spin_lock_init(&device->io_lock);
  1452. device->generation = trans->transid;
  1453. device->io_width = root->sectorsize;
  1454. device->io_align = root->sectorsize;
  1455. device->sector_size = root->sectorsize;
  1456. device->total_bytes = i_size_read(bdev->bd_inode);
  1457. device->disk_total_bytes = device->total_bytes;
  1458. device->dev_root = root->fs_info->dev_root;
  1459. device->bdev = bdev;
  1460. device->in_fs_metadata = 1;
  1461. device->mode = FMODE_EXCL;
  1462. set_blocksize(device->bdev, 4096);
  1463. if (seeding_dev) {
  1464. sb->s_flags &= ~MS_RDONLY;
  1465. ret = btrfs_prepare_sprout(trans, root);
  1466. BUG_ON(ret);
  1467. }
  1468. device->fs_devices = root->fs_info->fs_devices;
  1469. /*
  1470. * we don't want write_supers to jump in here with our device
  1471. * half setup
  1472. */
  1473. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1474. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1475. list_add(&device->dev_alloc_list,
  1476. &root->fs_info->fs_devices->alloc_list);
  1477. root->fs_info->fs_devices->num_devices++;
  1478. root->fs_info->fs_devices->open_devices++;
  1479. root->fs_info->fs_devices->rw_devices++;
  1480. if (device->can_discard)
  1481. root->fs_info->fs_devices->num_can_discard++;
  1482. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1483. spin_lock(&root->fs_info->free_chunk_lock);
  1484. root->fs_info->free_chunk_space += device->total_bytes;
  1485. spin_unlock(&root->fs_info->free_chunk_lock);
  1486. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1487. root->fs_info->fs_devices->rotating = 1;
  1488. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1489. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1490. total_bytes + device->total_bytes);
  1491. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1492. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1493. total_bytes + 1);
  1494. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1495. if (seeding_dev) {
  1496. ret = init_first_rw_device(trans, root, device);
  1497. BUG_ON(ret);
  1498. ret = btrfs_finish_sprout(trans, root);
  1499. BUG_ON(ret);
  1500. } else {
  1501. ret = btrfs_add_device(trans, root, device);
  1502. }
  1503. /*
  1504. * we've got more storage, clear any full flags on the space
  1505. * infos
  1506. */
  1507. btrfs_clear_space_info_full(root->fs_info);
  1508. unlock_chunks(root);
  1509. btrfs_commit_transaction(trans, root);
  1510. if (seeding_dev) {
  1511. mutex_unlock(&uuid_mutex);
  1512. up_write(&sb->s_umount);
  1513. ret = btrfs_relocate_sys_chunks(root);
  1514. BUG_ON(ret);
  1515. }
  1516. out:
  1517. mutex_unlock(&root->fs_info->volume_mutex);
  1518. return ret;
  1519. error:
  1520. blkdev_put(bdev, FMODE_EXCL);
  1521. if (seeding_dev) {
  1522. mutex_unlock(&uuid_mutex);
  1523. up_write(&sb->s_umount);
  1524. }
  1525. goto out;
  1526. }
  1527. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1528. struct btrfs_device *device)
  1529. {
  1530. int ret;
  1531. struct btrfs_path *path;
  1532. struct btrfs_root *root;
  1533. struct btrfs_dev_item *dev_item;
  1534. struct extent_buffer *leaf;
  1535. struct btrfs_key key;
  1536. root = device->dev_root->fs_info->chunk_root;
  1537. path = btrfs_alloc_path();
  1538. if (!path)
  1539. return -ENOMEM;
  1540. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1541. key.type = BTRFS_DEV_ITEM_KEY;
  1542. key.offset = device->devid;
  1543. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1544. if (ret < 0)
  1545. goto out;
  1546. if (ret > 0) {
  1547. ret = -ENOENT;
  1548. goto out;
  1549. }
  1550. leaf = path->nodes[0];
  1551. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1552. btrfs_set_device_id(leaf, dev_item, device->devid);
  1553. btrfs_set_device_type(leaf, dev_item, device->type);
  1554. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1555. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1556. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1557. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1558. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1559. btrfs_mark_buffer_dirty(leaf);
  1560. out:
  1561. btrfs_free_path(path);
  1562. return ret;
  1563. }
  1564. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1565. struct btrfs_device *device, u64 new_size)
  1566. {
  1567. struct btrfs_super_block *super_copy =
  1568. device->dev_root->fs_info->super_copy;
  1569. u64 old_total = btrfs_super_total_bytes(super_copy);
  1570. u64 diff = new_size - device->total_bytes;
  1571. if (!device->writeable)
  1572. return -EACCES;
  1573. if (new_size <= device->total_bytes)
  1574. return -EINVAL;
  1575. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1576. device->fs_devices->total_rw_bytes += diff;
  1577. device->total_bytes = new_size;
  1578. device->disk_total_bytes = new_size;
  1579. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1580. return btrfs_update_device(trans, device);
  1581. }
  1582. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1583. struct btrfs_device *device, u64 new_size)
  1584. {
  1585. int ret;
  1586. lock_chunks(device->dev_root);
  1587. ret = __btrfs_grow_device(trans, device, new_size);
  1588. unlock_chunks(device->dev_root);
  1589. return ret;
  1590. }
  1591. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1592. struct btrfs_root *root,
  1593. u64 chunk_tree, u64 chunk_objectid,
  1594. u64 chunk_offset)
  1595. {
  1596. int ret;
  1597. struct btrfs_path *path;
  1598. struct btrfs_key key;
  1599. root = root->fs_info->chunk_root;
  1600. path = btrfs_alloc_path();
  1601. if (!path)
  1602. return -ENOMEM;
  1603. key.objectid = chunk_objectid;
  1604. key.offset = chunk_offset;
  1605. key.type = BTRFS_CHUNK_ITEM_KEY;
  1606. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1607. BUG_ON(ret);
  1608. ret = btrfs_del_item(trans, root, path);
  1609. btrfs_free_path(path);
  1610. return ret;
  1611. }
  1612. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1613. chunk_offset)
  1614. {
  1615. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1616. struct btrfs_disk_key *disk_key;
  1617. struct btrfs_chunk *chunk;
  1618. u8 *ptr;
  1619. int ret = 0;
  1620. u32 num_stripes;
  1621. u32 array_size;
  1622. u32 len = 0;
  1623. u32 cur;
  1624. struct btrfs_key key;
  1625. array_size = btrfs_super_sys_array_size(super_copy);
  1626. ptr = super_copy->sys_chunk_array;
  1627. cur = 0;
  1628. while (cur < array_size) {
  1629. disk_key = (struct btrfs_disk_key *)ptr;
  1630. btrfs_disk_key_to_cpu(&key, disk_key);
  1631. len = sizeof(*disk_key);
  1632. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1633. chunk = (struct btrfs_chunk *)(ptr + len);
  1634. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1635. len += btrfs_chunk_item_size(num_stripes);
  1636. } else {
  1637. ret = -EIO;
  1638. break;
  1639. }
  1640. if (key.objectid == chunk_objectid &&
  1641. key.offset == chunk_offset) {
  1642. memmove(ptr, ptr + len, array_size - (cur + len));
  1643. array_size -= len;
  1644. btrfs_set_super_sys_array_size(super_copy, array_size);
  1645. } else {
  1646. ptr += len;
  1647. cur += len;
  1648. }
  1649. }
  1650. return ret;
  1651. }
  1652. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1653. u64 chunk_tree, u64 chunk_objectid,
  1654. u64 chunk_offset)
  1655. {
  1656. struct extent_map_tree *em_tree;
  1657. struct btrfs_root *extent_root;
  1658. struct btrfs_trans_handle *trans;
  1659. struct extent_map *em;
  1660. struct map_lookup *map;
  1661. int ret;
  1662. int i;
  1663. root = root->fs_info->chunk_root;
  1664. extent_root = root->fs_info->extent_root;
  1665. em_tree = &root->fs_info->mapping_tree.map_tree;
  1666. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1667. if (ret)
  1668. return -ENOSPC;
  1669. /* step one, relocate all the extents inside this chunk */
  1670. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1671. if (ret)
  1672. return ret;
  1673. trans = btrfs_start_transaction(root, 0);
  1674. BUG_ON(IS_ERR(trans));
  1675. lock_chunks(root);
  1676. /*
  1677. * step two, delete the device extents and the
  1678. * chunk tree entries
  1679. */
  1680. read_lock(&em_tree->lock);
  1681. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1682. read_unlock(&em_tree->lock);
  1683. BUG_ON(em->start > chunk_offset ||
  1684. em->start + em->len < chunk_offset);
  1685. map = (struct map_lookup *)em->bdev;
  1686. for (i = 0; i < map->num_stripes; i++) {
  1687. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1688. map->stripes[i].physical);
  1689. BUG_ON(ret);
  1690. if (map->stripes[i].dev) {
  1691. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1692. BUG_ON(ret);
  1693. }
  1694. }
  1695. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1696. chunk_offset);
  1697. BUG_ON(ret);
  1698. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1699. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1700. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1701. BUG_ON(ret);
  1702. }
  1703. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1704. BUG_ON(ret);
  1705. write_lock(&em_tree->lock);
  1706. remove_extent_mapping(em_tree, em);
  1707. write_unlock(&em_tree->lock);
  1708. kfree(map);
  1709. em->bdev = NULL;
  1710. /* once for the tree */
  1711. free_extent_map(em);
  1712. /* once for us */
  1713. free_extent_map(em);
  1714. unlock_chunks(root);
  1715. btrfs_end_transaction(trans, root);
  1716. return 0;
  1717. }
  1718. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1719. {
  1720. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1721. struct btrfs_path *path;
  1722. struct extent_buffer *leaf;
  1723. struct btrfs_chunk *chunk;
  1724. struct btrfs_key key;
  1725. struct btrfs_key found_key;
  1726. u64 chunk_tree = chunk_root->root_key.objectid;
  1727. u64 chunk_type;
  1728. bool retried = false;
  1729. int failed = 0;
  1730. int ret;
  1731. path = btrfs_alloc_path();
  1732. if (!path)
  1733. return -ENOMEM;
  1734. again:
  1735. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1736. key.offset = (u64)-1;
  1737. key.type = BTRFS_CHUNK_ITEM_KEY;
  1738. while (1) {
  1739. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1740. if (ret < 0)
  1741. goto error;
  1742. BUG_ON(ret == 0);
  1743. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1744. key.type);
  1745. if (ret < 0)
  1746. goto error;
  1747. if (ret > 0)
  1748. break;
  1749. leaf = path->nodes[0];
  1750. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1751. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1752. struct btrfs_chunk);
  1753. chunk_type = btrfs_chunk_type(leaf, chunk);
  1754. btrfs_release_path(path);
  1755. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1756. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1757. found_key.objectid,
  1758. found_key.offset);
  1759. if (ret == -ENOSPC)
  1760. failed++;
  1761. else if (ret)
  1762. BUG();
  1763. }
  1764. if (found_key.offset == 0)
  1765. break;
  1766. key.offset = found_key.offset - 1;
  1767. }
  1768. ret = 0;
  1769. if (failed && !retried) {
  1770. failed = 0;
  1771. retried = true;
  1772. goto again;
  1773. } else if (failed && retried) {
  1774. WARN_ON(1);
  1775. ret = -ENOSPC;
  1776. }
  1777. error:
  1778. btrfs_free_path(path);
  1779. return ret;
  1780. }
  1781. static u64 div_factor(u64 num, int factor)
  1782. {
  1783. if (factor == 10)
  1784. return num;
  1785. num *= factor;
  1786. do_div(num, 10);
  1787. return num;
  1788. }
  1789. int btrfs_balance(struct btrfs_root *dev_root)
  1790. {
  1791. int ret;
  1792. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1793. struct btrfs_device *device;
  1794. u64 old_size;
  1795. u64 size_to_free;
  1796. struct btrfs_path *path;
  1797. struct btrfs_key key;
  1798. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1799. struct btrfs_trans_handle *trans;
  1800. struct btrfs_key found_key;
  1801. if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
  1802. return -EROFS;
  1803. if (!capable(CAP_SYS_ADMIN))
  1804. return -EPERM;
  1805. mutex_lock(&dev_root->fs_info->volume_mutex);
  1806. dev_root = dev_root->fs_info->dev_root;
  1807. /* step one make some room on all the devices */
  1808. list_for_each_entry(device, devices, dev_list) {
  1809. old_size = device->total_bytes;
  1810. size_to_free = div_factor(old_size, 1);
  1811. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1812. if (!device->writeable ||
  1813. device->total_bytes - device->bytes_used > size_to_free)
  1814. continue;
  1815. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1816. if (ret == -ENOSPC)
  1817. break;
  1818. BUG_ON(ret);
  1819. trans = btrfs_start_transaction(dev_root, 0);
  1820. BUG_ON(IS_ERR(trans));
  1821. ret = btrfs_grow_device(trans, device, old_size);
  1822. BUG_ON(ret);
  1823. btrfs_end_transaction(trans, dev_root);
  1824. }
  1825. /* step two, relocate all the chunks */
  1826. path = btrfs_alloc_path();
  1827. if (!path) {
  1828. ret = -ENOMEM;
  1829. goto error;
  1830. }
  1831. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1832. key.offset = (u64)-1;
  1833. key.type = BTRFS_CHUNK_ITEM_KEY;
  1834. while (1) {
  1835. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1836. if (ret < 0)
  1837. goto error;
  1838. /*
  1839. * this shouldn't happen, it means the last relocate
  1840. * failed
  1841. */
  1842. if (ret == 0)
  1843. break;
  1844. ret = btrfs_previous_item(chunk_root, path, 0,
  1845. BTRFS_CHUNK_ITEM_KEY);
  1846. if (ret)
  1847. break;
  1848. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1849. path->slots[0]);
  1850. if (found_key.objectid != key.objectid)
  1851. break;
  1852. /* chunk zero is special */
  1853. if (found_key.offset == 0)
  1854. break;
  1855. btrfs_release_path(path);
  1856. ret = btrfs_relocate_chunk(chunk_root,
  1857. chunk_root->root_key.objectid,
  1858. found_key.objectid,
  1859. found_key.offset);
  1860. if (ret && ret != -ENOSPC)
  1861. goto error;
  1862. key.offset = found_key.offset - 1;
  1863. }
  1864. ret = 0;
  1865. error:
  1866. btrfs_free_path(path);
  1867. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1868. return ret;
  1869. }
  1870. /*
  1871. * shrinking a device means finding all of the device extents past
  1872. * the new size, and then following the back refs to the chunks.
  1873. * The chunk relocation code actually frees the device extent
  1874. */
  1875. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1876. {
  1877. struct btrfs_trans_handle *trans;
  1878. struct btrfs_root *root = device->dev_root;
  1879. struct btrfs_dev_extent *dev_extent = NULL;
  1880. struct btrfs_path *path;
  1881. u64 length;
  1882. u64 chunk_tree;
  1883. u64 chunk_objectid;
  1884. u64 chunk_offset;
  1885. int ret;
  1886. int slot;
  1887. int failed = 0;
  1888. bool retried = false;
  1889. struct extent_buffer *l;
  1890. struct btrfs_key key;
  1891. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1892. u64 old_total = btrfs_super_total_bytes(super_copy);
  1893. u64 old_size = device->total_bytes;
  1894. u64 diff = device->total_bytes - new_size;
  1895. if (new_size >= device->total_bytes)
  1896. return -EINVAL;
  1897. path = btrfs_alloc_path();
  1898. if (!path)
  1899. return -ENOMEM;
  1900. path->reada = 2;
  1901. lock_chunks(root);
  1902. device->total_bytes = new_size;
  1903. if (device->writeable) {
  1904. device->fs_devices->total_rw_bytes -= diff;
  1905. spin_lock(&root->fs_info->free_chunk_lock);
  1906. root->fs_info->free_chunk_space -= diff;
  1907. spin_unlock(&root->fs_info->free_chunk_lock);
  1908. }
  1909. unlock_chunks(root);
  1910. again:
  1911. key.objectid = device->devid;
  1912. key.offset = (u64)-1;
  1913. key.type = BTRFS_DEV_EXTENT_KEY;
  1914. while (1) {
  1915. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1916. if (ret < 0)
  1917. goto done;
  1918. ret = btrfs_previous_item(root, path, 0, key.type);
  1919. if (ret < 0)
  1920. goto done;
  1921. if (ret) {
  1922. ret = 0;
  1923. btrfs_release_path(path);
  1924. break;
  1925. }
  1926. l = path->nodes[0];
  1927. slot = path->slots[0];
  1928. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1929. if (key.objectid != device->devid) {
  1930. btrfs_release_path(path);
  1931. break;
  1932. }
  1933. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1934. length = btrfs_dev_extent_length(l, dev_extent);
  1935. if (key.offset + length <= new_size) {
  1936. btrfs_release_path(path);
  1937. break;
  1938. }
  1939. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1940. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1941. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1942. btrfs_release_path(path);
  1943. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1944. chunk_offset);
  1945. if (ret && ret != -ENOSPC)
  1946. goto done;
  1947. if (ret == -ENOSPC)
  1948. failed++;
  1949. key.offset -= 1;
  1950. }
  1951. if (failed && !retried) {
  1952. failed = 0;
  1953. retried = true;
  1954. goto again;
  1955. } else if (failed && retried) {
  1956. ret = -ENOSPC;
  1957. lock_chunks(root);
  1958. device->total_bytes = old_size;
  1959. if (device->writeable)
  1960. device->fs_devices->total_rw_bytes += diff;
  1961. spin_lock(&root->fs_info->free_chunk_lock);
  1962. root->fs_info->free_chunk_space += diff;
  1963. spin_unlock(&root->fs_info->free_chunk_lock);
  1964. unlock_chunks(root);
  1965. goto done;
  1966. }
  1967. /* Shrinking succeeded, else we would be at "done". */
  1968. trans = btrfs_start_transaction(root, 0);
  1969. if (IS_ERR(trans)) {
  1970. ret = PTR_ERR(trans);
  1971. goto done;
  1972. }
  1973. lock_chunks(root);
  1974. device->disk_total_bytes = new_size;
  1975. /* Now btrfs_update_device() will change the on-disk size. */
  1976. ret = btrfs_update_device(trans, device);
  1977. if (ret) {
  1978. unlock_chunks(root);
  1979. btrfs_end_transaction(trans, root);
  1980. goto done;
  1981. }
  1982. WARN_ON(diff > old_total);
  1983. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1984. unlock_chunks(root);
  1985. btrfs_end_transaction(trans, root);
  1986. done:
  1987. btrfs_free_path(path);
  1988. return ret;
  1989. }
  1990. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1991. struct btrfs_root *root,
  1992. struct btrfs_key *key,
  1993. struct btrfs_chunk *chunk, int item_size)
  1994. {
  1995. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1996. struct btrfs_disk_key disk_key;
  1997. u32 array_size;
  1998. u8 *ptr;
  1999. array_size = btrfs_super_sys_array_size(super_copy);
  2000. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2001. return -EFBIG;
  2002. ptr = super_copy->sys_chunk_array + array_size;
  2003. btrfs_cpu_key_to_disk(&disk_key, key);
  2004. memcpy(ptr, &disk_key, sizeof(disk_key));
  2005. ptr += sizeof(disk_key);
  2006. memcpy(ptr, chunk, item_size);
  2007. item_size += sizeof(disk_key);
  2008. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2009. return 0;
  2010. }
  2011. /*
  2012. * sort the devices in descending order by max_avail, total_avail
  2013. */
  2014. static int btrfs_cmp_device_info(const void *a, const void *b)
  2015. {
  2016. const struct btrfs_device_info *di_a = a;
  2017. const struct btrfs_device_info *di_b = b;
  2018. if (di_a->max_avail > di_b->max_avail)
  2019. return -1;
  2020. if (di_a->max_avail < di_b->max_avail)
  2021. return 1;
  2022. if (di_a->total_avail > di_b->total_avail)
  2023. return -1;
  2024. if (di_a->total_avail < di_b->total_avail)
  2025. return 1;
  2026. return 0;
  2027. }
  2028. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2029. struct btrfs_root *extent_root,
  2030. struct map_lookup **map_ret,
  2031. u64 *num_bytes_out, u64 *stripe_size_out,
  2032. u64 start, u64 type)
  2033. {
  2034. struct btrfs_fs_info *info = extent_root->fs_info;
  2035. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2036. struct list_head *cur;
  2037. struct map_lookup *map = NULL;
  2038. struct extent_map_tree *em_tree;
  2039. struct extent_map *em;
  2040. struct btrfs_device_info *devices_info = NULL;
  2041. u64 total_avail;
  2042. int num_stripes; /* total number of stripes to allocate */
  2043. int sub_stripes; /* sub_stripes info for map */
  2044. int dev_stripes; /* stripes per dev */
  2045. int devs_max; /* max devs to use */
  2046. int devs_min; /* min devs needed */
  2047. int devs_increment; /* ndevs has to be a multiple of this */
  2048. int ncopies; /* how many copies to data has */
  2049. int ret;
  2050. u64 max_stripe_size;
  2051. u64 max_chunk_size;
  2052. u64 stripe_size;
  2053. u64 num_bytes;
  2054. int ndevs;
  2055. int i;
  2056. int j;
  2057. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2058. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2059. WARN_ON(1);
  2060. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2061. }
  2062. if (list_empty(&fs_devices->alloc_list))
  2063. return -ENOSPC;
  2064. sub_stripes = 1;
  2065. dev_stripes = 1;
  2066. devs_increment = 1;
  2067. ncopies = 1;
  2068. devs_max = 0; /* 0 == as many as possible */
  2069. devs_min = 1;
  2070. /*
  2071. * define the properties of each RAID type.
  2072. * FIXME: move this to a global table and use it in all RAID
  2073. * calculation code
  2074. */
  2075. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2076. dev_stripes = 2;
  2077. ncopies = 2;
  2078. devs_max = 1;
  2079. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2080. devs_min = 2;
  2081. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2082. devs_increment = 2;
  2083. ncopies = 2;
  2084. devs_max = 2;
  2085. devs_min = 2;
  2086. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2087. sub_stripes = 2;
  2088. devs_increment = 2;
  2089. ncopies = 2;
  2090. devs_min = 4;
  2091. } else {
  2092. devs_max = 1;
  2093. }
  2094. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2095. max_stripe_size = 1024 * 1024 * 1024;
  2096. max_chunk_size = 10 * max_stripe_size;
  2097. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2098. max_stripe_size = 256 * 1024 * 1024;
  2099. max_chunk_size = max_stripe_size;
  2100. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2101. max_stripe_size = 8 * 1024 * 1024;
  2102. max_chunk_size = 2 * max_stripe_size;
  2103. } else {
  2104. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2105. type);
  2106. BUG_ON(1);
  2107. }
  2108. /* we don't want a chunk larger than 10% of writeable space */
  2109. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2110. max_chunk_size);
  2111. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2112. GFP_NOFS);
  2113. if (!devices_info)
  2114. return -ENOMEM;
  2115. cur = fs_devices->alloc_list.next;
  2116. /*
  2117. * in the first pass through the devices list, we gather information
  2118. * about the available holes on each device.
  2119. */
  2120. ndevs = 0;
  2121. while (cur != &fs_devices->alloc_list) {
  2122. struct btrfs_device *device;
  2123. u64 max_avail;
  2124. u64 dev_offset;
  2125. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2126. cur = cur->next;
  2127. if (!device->writeable) {
  2128. printk(KERN_ERR
  2129. "btrfs: read-only device in alloc_list\n");
  2130. WARN_ON(1);
  2131. continue;
  2132. }
  2133. if (!device->in_fs_metadata)
  2134. continue;
  2135. if (device->total_bytes > device->bytes_used)
  2136. total_avail = device->total_bytes - device->bytes_used;
  2137. else
  2138. total_avail = 0;
  2139. /* If there is no space on this device, skip it. */
  2140. if (total_avail == 0)
  2141. continue;
  2142. ret = find_free_dev_extent(trans, device,
  2143. max_stripe_size * dev_stripes,
  2144. &dev_offset, &max_avail);
  2145. if (ret && ret != -ENOSPC)
  2146. goto error;
  2147. if (ret == 0)
  2148. max_avail = max_stripe_size * dev_stripes;
  2149. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2150. continue;
  2151. devices_info[ndevs].dev_offset = dev_offset;
  2152. devices_info[ndevs].max_avail = max_avail;
  2153. devices_info[ndevs].total_avail = total_avail;
  2154. devices_info[ndevs].dev = device;
  2155. ++ndevs;
  2156. }
  2157. /*
  2158. * now sort the devices by hole size / available space
  2159. */
  2160. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2161. btrfs_cmp_device_info, NULL);
  2162. /* round down to number of usable stripes */
  2163. ndevs -= ndevs % devs_increment;
  2164. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2165. ret = -ENOSPC;
  2166. goto error;
  2167. }
  2168. if (devs_max && ndevs > devs_max)
  2169. ndevs = devs_max;
  2170. /*
  2171. * the primary goal is to maximize the number of stripes, so use as many
  2172. * devices as possible, even if the stripes are not maximum sized.
  2173. */
  2174. stripe_size = devices_info[ndevs-1].max_avail;
  2175. num_stripes = ndevs * dev_stripes;
  2176. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2177. stripe_size = max_chunk_size * ncopies;
  2178. do_div(stripe_size, num_stripes);
  2179. }
  2180. do_div(stripe_size, dev_stripes);
  2181. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2182. stripe_size *= BTRFS_STRIPE_LEN;
  2183. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2184. if (!map) {
  2185. ret = -ENOMEM;
  2186. goto error;
  2187. }
  2188. map->num_stripes = num_stripes;
  2189. for (i = 0; i < ndevs; ++i) {
  2190. for (j = 0; j < dev_stripes; ++j) {
  2191. int s = i * dev_stripes + j;
  2192. map->stripes[s].dev = devices_info[i].dev;
  2193. map->stripes[s].physical = devices_info[i].dev_offset +
  2194. j * stripe_size;
  2195. }
  2196. }
  2197. map->sector_size = extent_root->sectorsize;
  2198. map->stripe_len = BTRFS_STRIPE_LEN;
  2199. map->io_align = BTRFS_STRIPE_LEN;
  2200. map->io_width = BTRFS_STRIPE_LEN;
  2201. map->type = type;
  2202. map->sub_stripes = sub_stripes;
  2203. *map_ret = map;
  2204. num_bytes = stripe_size * (num_stripes / ncopies);
  2205. *stripe_size_out = stripe_size;
  2206. *num_bytes_out = num_bytes;
  2207. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2208. em = alloc_extent_map();
  2209. if (!em) {
  2210. ret = -ENOMEM;
  2211. goto error;
  2212. }
  2213. em->bdev = (struct block_device *)map;
  2214. em->start = start;
  2215. em->len = num_bytes;
  2216. em->block_start = 0;
  2217. em->block_len = em->len;
  2218. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2219. write_lock(&em_tree->lock);
  2220. ret = add_extent_mapping(em_tree, em);
  2221. write_unlock(&em_tree->lock);
  2222. BUG_ON(ret);
  2223. free_extent_map(em);
  2224. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2225. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2226. start, num_bytes);
  2227. BUG_ON(ret);
  2228. for (i = 0; i < map->num_stripes; ++i) {
  2229. struct btrfs_device *device;
  2230. u64 dev_offset;
  2231. device = map->stripes[i].dev;
  2232. dev_offset = map->stripes[i].physical;
  2233. ret = btrfs_alloc_dev_extent(trans, device,
  2234. info->chunk_root->root_key.objectid,
  2235. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2236. start, dev_offset, stripe_size);
  2237. BUG_ON(ret);
  2238. }
  2239. kfree(devices_info);
  2240. return 0;
  2241. error:
  2242. kfree(map);
  2243. kfree(devices_info);
  2244. return ret;
  2245. }
  2246. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2247. struct btrfs_root *extent_root,
  2248. struct map_lookup *map, u64 chunk_offset,
  2249. u64 chunk_size, u64 stripe_size)
  2250. {
  2251. u64 dev_offset;
  2252. struct btrfs_key key;
  2253. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2254. struct btrfs_device *device;
  2255. struct btrfs_chunk *chunk;
  2256. struct btrfs_stripe *stripe;
  2257. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2258. int index = 0;
  2259. int ret;
  2260. chunk = kzalloc(item_size, GFP_NOFS);
  2261. if (!chunk)
  2262. return -ENOMEM;
  2263. index = 0;
  2264. while (index < map->num_stripes) {
  2265. device = map->stripes[index].dev;
  2266. device->bytes_used += stripe_size;
  2267. ret = btrfs_update_device(trans, device);
  2268. BUG_ON(ret);
  2269. index++;
  2270. }
  2271. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2272. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2273. map->num_stripes);
  2274. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2275. index = 0;
  2276. stripe = &chunk->stripe;
  2277. while (index < map->num_stripes) {
  2278. device = map->stripes[index].dev;
  2279. dev_offset = map->stripes[index].physical;
  2280. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2281. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2282. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2283. stripe++;
  2284. index++;
  2285. }
  2286. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2287. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2288. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2289. btrfs_set_stack_chunk_type(chunk, map->type);
  2290. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2291. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2292. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2293. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2294. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2295. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2296. key.type = BTRFS_CHUNK_ITEM_KEY;
  2297. key.offset = chunk_offset;
  2298. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2299. BUG_ON(ret);
  2300. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2301. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2302. item_size);
  2303. BUG_ON(ret);
  2304. }
  2305. kfree(chunk);
  2306. return 0;
  2307. }
  2308. /*
  2309. * Chunk allocation falls into two parts. The first part does works
  2310. * that make the new allocated chunk useable, but not do any operation
  2311. * that modifies the chunk tree. The second part does the works that
  2312. * require modifying the chunk tree. This division is important for the
  2313. * bootstrap process of adding storage to a seed btrfs.
  2314. */
  2315. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2316. struct btrfs_root *extent_root, u64 type)
  2317. {
  2318. u64 chunk_offset;
  2319. u64 chunk_size;
  2320. u64 stripe_size;
  2321. struct map_lookup *map;
  2322. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2323. int ret;
  2324. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2325. &chunk_offset);
  2326. if (ret)
  2327. return ret;
  2328. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2329. &stripe_size, chunk_offset, type);
  2330. if (ret)
  2331. return ret;
  2332. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2333. chunk_size, stripe_size);
  2334. BUG_ON(ret);
  2335. return 0;
  2336. }
  2337. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2338. struct btrfs_root *root,
  2339. struct btrfs_device *device)
  2340. {
  2341. u64 chunk_offset;
  2342. u64 sys_chunk_offset;
  2343. u64 chunk_size;
  2344. u64 sys_chunk_size;
  2345. u64 stripe_size;
  2346. u64 sys_stripe_size;
  2347. u64 alloc_profile;
  2348. struct map_lookup *map;
  2349. struct map_lookup *sys_map;
  2350. struct btrfs_fs_info *fs_info = root->fs_info;
  2351. struct btrfs_root *extent_root = fs_info->extent_root;
  2352. int ret;
  2353. ret = find_next_chunk(fs_info->chunk_root,
  2354. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2355. if (ret)
  2356. return ret;
  2357. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2358. (fs_info->metadata_alloc_profile &
  2359. fs_info->avail_metadata_alloc_bits);
  2360. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2361. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2362. &stripe_size, chunk_offset, alloc_profile);
  2363. BUG_ON(ret);
  2364. sys_chunk_offset = chunk_offset + chunk_size;
  2365. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2366. (fs_info->system_alloc_profile &
  2367. fs_info->avail_system_alloc_bits);
  2368. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2369. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2370. &sys_chunk_size, &sys_stripe_size,
  2371. sys_chunk_offset, alloc_profile);
  2372. BUG_ON(ret);
  2373. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2374. BUG_ON(ret);
  2375. /*
  2376. * Modifying chunk tree needs allocating new blocks from both
  2377. * system block group and metadata block group. So we only can
  2378. * do operations require modifying the chunk tree after both
  2379. * block groups were created.
  2380. */
  2381. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2382. chunk_size, stripe_size);
  2383. BUG_ON(ret);
  2384. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2385. sys_chunk_offset, sys_chunk_size,
  2386. sys_stripe_size);
  2387. BUG_ON(ret);
  2388. return 0;
  2389. }
  2390. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2391. {
  2392. struct extent_map *em;
  2393. struct map_lookup *map;
  2394. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2395. int readonly = 0;
  2396. int i;
  2397. read_lock(&map_tree->map_tree.lock);
  2398. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2399. read_unlock(&map_tree->map_tree.lock);
  2400. if (!em)
  2401. return 1;
  2402. if (btrfs_test_opt(root, DEGRADED)) {
  2403. free_extent_map(em);
  2404. return 0;
  2405. }
  2406. map = (struct map_lookup *)em->bdev;
  2407. for (i = 0; i < map->num_stripes; i++) {
  2408. if (!map->stripes[i].dev->writeable) {
  2409. readonly = 1;
  2410. break;
  2411. }
  2412. }
  2413. free_extent_map(em);
  2414. return readonly;
  2415. }
  2416. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2417. {
  2418. extent_map_tree_init(&tree->map_tree);
  2419. }
  2420. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2421. {
  2422. struct extent_map *em;
  2423. while (1) {
  2424. write_lock(&tree->map_tree.lock);
  2425. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2426. if (em)
  2427. remove_extent_mapping(&tree->map_tree, em);
  2428. write_unlock(&tree->map_tree.lock);
  2429. if (!em)
  2430. break;
  2431. kfree(em->bdev);
  2432. /* once for us */
  2433. free_extent_map(em);
  2434. /* once for the tree */
  2435. free_extent_map(em);
  2436. }
  2437. }
  2438. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2439. {
  2440. struct extent_map *em;
  2441. struct map_lookup *map;
  2442. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2443. int ret;
  2444. read_lock(&em_tree->lock);
  2445. em = lookup_extent_mapping(em_tree, logical, len);
  2446. read_unlock(&em_tree->lock);
  2447. BUG_ON(!em);
  2448. BUG_ON(em->start > logical || em->start + em->len < logical);
  2449. map = (struct map_lookup *)em->bdev;
  2450. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2451. ret = map->num_stripes;
  2452. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2453. ret = map->sub_stripes;
  2454. else
  2455. ret = 1;
  2456. free_extent_map(em);
  2457. return ret;
  2458. }
  2459. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2460. int optimal)
  2461. {
  2462. int i;
  2463. if (map->stripes[optimal].dev->bdev)
  2464. return optimal;
  2465. for (i = first; i < first + num; i++) {
  2466. if (map->stripes[i].dev->bdev)
  2467. return i;
  2468. }
  2469. /* we couldn't find one that doesn't fail. Just return something
  2470. * and the io error handling code will clean up eventually
  2471. */
  2472. return optimal;
  2473. }
  2474. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2475. u64 logical, u64 *length,
  2476. struct btrfs_bio **bbio_ret,
  2477. int mirror_num)
  2478. {
  2479. struct extent_map *em;
  2480. struct map_lookup *map;
  2481. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2482. u64 offset;
  2483. u64 stripe_offset;
  2484. u64 stripe_end_offset;
  2485. u64 stripe_nr;
  2486. u64 stripe_nr_orig;
  2487. u64 stripe_nr_end;
  2488. int stripes_allocated = 8;
  2489. int stripes_required = 1;
  2490. int stripe_index;
  2491. int i;
  2492. int num_stripes;
  2493. int max_errors = 0;
  2494. struct btrfs_bio *bbio = NULL;
  2495. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2496. stripes_allocated = 1;
  2497. again:
  2498. if (bbio_ret) {
  2499. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  2500. GFP_NOFS);
  2501. if (!bbio)
  2502. return -ENOMEM;
  2503. atomic_set(&bbio->error, 0);
  2504. }
  2505. read_lock(&em_tree->lock);
  2506. em = lookup_extent_mapping(em_tree, logical, *length);
  2507. read_unlock(&em_tree->lock);
  2508. if (!em) {
  2509. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2510. (unsigned long long)logical,
  2511. (unsigned long long)*length);
  2512. BUG();
  2513. }
  2514. BUG_ON(em->start > logical || em->start + em->len < logical);
  2515. map = (struct map_lookup *)em->bdev;
  2516. offset = logical - em->start;
  2517. if (mirror_num > map->num_stripes)
  2518. mirror_num = 0;
  2519. /* if our btrfs_bio struct is too small, back off and try again */
  2520. if (rw & REQ_WRITE) {
  2521. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2522. BTRFS_BLOCK_GROUP_DUP)) {
  2523. stripes_required = map->num_stripes;
  2524. max_errors = 1;
  2525. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2526. stripes_required = map->sub_stripes;
  2527. max_errors = 1;
  2528. }
  2529. }
  2530. if (rw & REQ_DISCARD) {
  2531. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2532. BTRFS_BLOCK_GROUP_RAID1 |
  2533. BTRFS_BLOCK_GROUP_DUP |
  2534. BTRFS_BLOCK_GROUP_RAID10)) {
  2535. stripes_required = map->num_stripes;
  2536. }
  2537. }
  2538. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  2539. stripes_allocated < stripes_required) {
  2540. stripes_allocated = map->num_stripes;
  2541. free_extent_map(em);
  2542. kfree(bbio);
  2543. goto again;
  2544. }
  2545. stripe_nr = offset;
  2546. /*
  2547. * stripe_nr counts the total number of stripes we have to stride
  2548. * to get to this block
  2549. */
  2550. do_div(stripe_nr, map->stripe_len);
  2551. stripe_offset = stripe_nr * map->stripe_len;
  2552. BUG_ON(offset < stripe_offset);
  2553. /* stripe_offset is the offset of this block in its stripe*/
  2554. stripe_offset = offset - stripe_offset;
  2555. if (rw & REQ_DISCARD)
  2556. *length = min_t(u64, em->len - offset, *length);
  2557. else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2558. BTRFS_BLOCK_GROUP_RAID1 |
  2559. BTRFS_BLOCK_GROUP_RAID10 |
  2560. BTRFS_BLOCK_GROUP_DUP)) {
  2561. /* we limit the length of each bio to what fits in a stripe */
  2562. *length = min_t(u64, em->len - offset,
  2563. map->stripe_len - stripe_offset);
  2564. } else {
  2565. *length = em->len - offset;
  2566. }
  2567. if (!bbio_ret)
  2568. goto out;
  2569. num_stripes = 1;
  2570. stripe_index = 0;
  2571. stripe_nr_orig = stripe_nr;
  2572. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  2573. (~(map->stripe_len - 1));
  2574. do_div(stripe_nr_end, map->stripe_len);
  2575. stripe_end_offset = stripe_nr_end * map->stripe_len -
  2576. (offset + *length);
  2577. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2578. if (rw & REQ_DISCARD)
  2579. num_stripes = min_t(u64, map->num_stripes,
  2580. stripe_nr_end - stripe_nr_orig);
  2581. stripe_index = do_div(stripe_nr, map->num_stripes);
  2582. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2583. if (rw & (REQ_WRITE | REQ_DISCARD))
  2584. num_stripes = map->num_stripes;
  2585. else if (mirror_num)
  2586. stripe_index = mirror_num - 1;
  2587. else {
  2588. stripe_index = find_live_mirror(map, 0,
  2589. map->num_stripes,
  2590. current->pid % map->num_stripes);
  2591. mirror_num = stripe_index + 1;
  2592. }
  2593. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2594. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  2595. num_stripes = map->num_stripes;
  2596. } else if (mirror_num) {
  2597. stripe_index = mirror_num - 1;
  2598. } else {
  2599. mirror_num = 1;
  2600. }
  2601. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2602. int factor = map->num_stripes / map->sub_stripes;
  2603. stripe_index = do_div(stripe_nr, factor);
  2604. stripe_index *= map->sub_stripes;
  2605. if (rw & REQ_WRITE)
  2606. num_stripes = map->sub_stripes;
  2607. else if (rw & REQ_DISCARD)
  2608. num_stripes = min_t(u64, map->sub_stripes *
  2609. (stripe_nr_end - stripe_nr_orig),
  2610. map->num_stripes);
  2611. else if (mirror_num)
  2612. stripe_index += mirror_num - 1;
  2613. else {
  2614. stripe_index = find_live_mirror(map, stripe_index,
  2615. map->sub_stripes, stripe_index +
  2616. current->pid % map->sub_stripes);
  2617. mirror_num = stripe_index + 1;
  2618. }
  2619. } else {
  2620. /*
  2621. * after this do_div call, stripe_nr is the number of stripes
  2622. * on this device we have to walk to find the data, and
  2623. * stripe_index is the number of our device in the stripe array
  2624. */
  2625. stripe_index = do_div(stripe_nr, map->num_stripes);
  2626. mirror_num = stripe_index + 1;
  2627. }
  2628. BUG_ON(stripe_index >= map->num_stripes);
  2629. if (rw & REQ_DISCARD) {
  2630. for (i = 0; i < num_stripes; i++) {
  2631. bbio->stripes[i].physical =
  2632. map->stripes[stripe_index].physical +
  2633. stripe_offset + stripe_nr * map->stripe_len;
  2634. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  2635. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2636. u64 stripes;
  2637. u32 last_stripe = 0;
  2638. int j;
  2639. div_u64_rem(stripe_nr_end - 1,
  2640. map->num_stripes,
  2641. &last_stripe);
  2642. for (j = 0; j < map->num_stripes; j++) {
  2643. u32 test;
  2644. div_u64_rem(stripe_nr_end - 1 - j,
  2645. map->num_stripes, &test);
  2646. if (test == stripe_index)
  2647. break;
  2648. }
  2649. stripes = stripe_nr_end - 1 - j;
  2650. do_div(stripes, map->num_stripes);
  2651. bbio->stripes[i].length = map->stripe_len *
  2652. (stripes - stripe_nr + 1);
  2653. if (i == 0) {
  2654. bbio->stripes[i].length -=
  2655. stripe_offset;
  2656. stripe_offset = 0;
  2657. }
  2658. if (stripe_index == last_stripe)
  2659. bbio->stripes[i].length -=
  2660. stripe_end_offset;
  2661. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2662. u64 stripes;
  2663. int j;
  2664. int factor = map->num_stripes /
  2665. map->sub_stripes;
  2666. u32 last_stripe = 0;
  2667. div_u64_rem(stripe_nr_end - 1,
  2668. factor, &last_stripe);
  2669. last_stripe *= map->sub_stripes;
  2670. for (j = 0; j < factor; j++) {
  2671. u32 test;
  2672. div_u64_rem(stripe_nr_end - 1 - j,
  2673. factor, &test);
  2674. if (test ==
  2675. stripe_index / map->sub_stripes)
  2676. break;
  2677. }
  2678. stripes = stripe_nr_end - 1 - j;
  2679. do_div(stripes, factor);
  2680. bbio->stripes[i].length = map->stripe_len *
  2681. (stripes - stripe_nr + 1);
  2682. if (i < map->sub_stripes) {
  2683. bbio->stripes[i].length -=
  2684. stripe_offset;
  2685. if (i == map->sub_stripes - 1)
  2686. stripe_offset = 0;
  2687. }
  2688. if (stripe_index >= last_stripe &&
  2689. stripe_index <= (last_stripe +
  2690. map->sub_stripes - 1)) {
  2691. bbio->stripes[i].length -=
  2692. stripe_end_offset;
  2693. }
  2694. } else
  2695. bbio->stripes[i].length = *length;
  2696. stripe_index++;
  2697. if (stripe_index == map->num_stripes) {
  2698. /* This could only happen for RAID0/10 */
  2699. stripe_index = 0;
  2700. stripe_nr++;
  2701. }
  2702. }
  2703. } else {
  2704. for (i = 0; i < num_stripes; i++) {
  2705. bbio->stripes[i].physical =
  2706. map->stripes[stripe_index].physical +
  2707. stripe_offset +
  2708. stripe_nr * map->stripe_len;
  2709. bbio->stripes[i].dev =
  2710. map->stripes[stripe_index].dev;
  2711. stripe_index++;
  2712. }
  2713. }
  2714. if (bbio_ret) {
  2715. *bbio_ret = bbio;
  2716. bbio->num_stripes = num_stripes;
  2717. bbio->max_errors = max_errors;
  2718. bbio->mirror_num = mirror_num;
  2719. }
  2720. out:
  2721. free_extent_map(em);
  2722. return 0;
  2723. }
  2724. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2725. u64 logical, u64 *length,
  2726. struct btrfs_bio **bbio_ret, int mirror_num)
  2727. {
  2728. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  2729. mirror_num);
  2730. }
  2731. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  2732. u64 chunk_start, u64 physical, u64 devid,
  2733. u64 **logical, int *naddrs, int *stripe_len)
  2734. {
  2735. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2736. struct extent_map *em;
  2737. struct map_lookup *map;
  2738. u64 *buf;
  2739. u64 bytenr;
  2740. u64 length;
  2741. u64 stripe_nr;
  2742. int i, j, nr = 0;
  2743. read_lock(&em_tree->lock);
  2744. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  2745. read_unlock(&em_tree->lock);
  2746. BUG_ON(!em || em->start != chunk_start);
  2747. map = (struct map_lookup *)em->bdev;
  2748. length = em->len;
  2749. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2750. do_div(length, map->num_stripes / map->sub_stripes);
  2751. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  2752. do_div(length, map->num_stripes);
  2753. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  2754. BUG_ON(!buf);
  2755. for (i = 0; i < map->num_stripes; i++) {
  2756. if (devid && map->stripes[i].dev->devid != devid)
  2757. continue;
  2758. if (map->stripes[i].physical > physical ||
  2759. map->stripes[i].physical + length <= physical)
  2760. continue;
  2761. stripe_nr = physical - map->stripes[i].physical;
  2762. do_div(stripe_nr, map->stripe_len);
  2763. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2764. stripe_nr = stripe_nr * map->num_stripes + i;
  2765. do_div(stripe_nr, map->sub_stripes);
  2766. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2767. stripe_nr = stripe_nr * map->num_stripes + i;
  2768. }
  2769. bytenr = chunk_start + stripe_nr * map->stripe_len;
  2770. WARN_ON(nr >= map->num_stripes);
  2771. for (j = 0; j < nr; j++) {
  2772. if (buf[j] == bytenr)
  2773. break;
  2774. }
  2775. if (j == nr) {
  2776. WARN_ON(nr >= map->num_stripes);
  2777. buf[nr++] = bytenr;
  2778. }
  2779. }
  2780. *logical = buf;
  2781. *naddrs = nr;
  2782. *stripe_len = map->stripe_len;
  2783. free_extent_map(em);
  2784. return 0;
  2785. }
  2786. static void btrfs_end_bio(struct bio *bio, int err)
  2787. {
  2788. struct btrfs_bio *bbio = bio->bi_private;
  2789. int is_orig_bio = 0;
  2790. if (err)
  2791. atomic_inc(&bbio->error);
  2792. if (bio == bbio->orig_bio)
  2793. is_orig_bio = 1;
  2794. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  2795. if (!is_orig_bio) {
  2796. bio_put(bio);
  2797. bio = bbio->orig_bio;
  2798. }
  2799. bio->bi_private = bbio->private;
  2800. bio->bi_end_io = bbio->end_io;
  2801. bio->bi_bdev = (struct block_device *)
  2802. (unsigned long)bbio->mirror_num;
  2803. /* only send an error to the higher layers if it is
  2804. * beyond the tolerance of the multi-bio
  2805. */
  2806. if (atomic_read(&bbio->error) > bbio->max_errors) {
  2807. err = -EIO;
  2808. } else {
  2809. /*
  2810. * this bio is actually up to date, we didn't
  2811. * go over the max number of errors
  2812. */
  2813. set_bit(BIO_UPTODATE, &bio->bi_flags);
  2814. err = 0;
  2815. }
  2816. kfree(bbio);
  2817. bio_endio(bio, err);
  2818. } else if (!is_orig_bio) {
  2819. bio_put(bio);
  2820. }
  2821. }
  2822. struct async_sched {
  2823. struct bio *bio;
  2824. int rw;
  2825. struct btrfs_fs_info *info;
  2826. struct btrfs_work work;
  2827. };
  2828. /*
  2829. * see run_scheduled_bios for a description of why bios are collected for
  2830. * async submit.
  2831. *
  2832. * This will add one bio to the pending list for a device and make sure
  2833. * the work struct is scheduled.
  2834. */
  2835. static noinline int schedule_bio(struct btrfs_root *root,
  2836. struct btrfs_device *device,
  2837. int rw, struct bio *bio)
  2838. {
  2839. int should_queue = 1;
  2840. struct btrfs_pending_bios *pending_bios;
  2841. /* don't bother with additional async steps for reads, right now */
  2842. if (!(rw & REQ_WRITE)) {
  2843. bio_get(bio);
  2844. btrfsic_submit_bio(rw, bio);
  2845. bio_put(bio);
  2846. return 0;
  2847. }
  2848. /*
  2849. * nr_async_bios allows us to reliably return congestion to the
  2850. * higher layers. Otherwise, the async bio makes it appear we have
  2851. * made progress against dirty pages when we've really just put it
  2852. * on a queue for later
  2853. */
  2854. atomic_inc(&root->fs_info->nr_async_bios);
  2855. WARN_ON(bio->bi_next);
  2856. bio->bi_next = NULL;
  2857. bio->bi_rw |= rw;
  2858. spin_lock(&device->io_lock);
  2859. if (bio->bi_rw & REQ_SYNC)
  2860. pending_bios = &device->pending_sync_bios;
  2861. else
  2862. pending_bios = &device->pending_bios;
  2863. if (pending_bios->tail)
  2864. pending_bios->tail->bi_next = bio;
  2865. pending_bios->tail = bio;
  2866. if (!pending_bios->head)
  2867. pending_bios->head = bio;
  2868. if (device->running_pending)
  2869. should_queue = 0;
  2870. spin_unlock(&device->io_lock);
  2871. if (should_queue)
  2872. btrfs_queue_worker(&root->fs_info->submit_workers,
  2873. &device->work);
  2874. return 0;
  2875. }
  2876. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  2877. int mirror_num, int async_submit)
  2878. {
  2879. struct btrfs_mapping_tree *map_tree;
  2880. struct btrfs_device *dev;
  2881. struct bio *first_bio = bio;
  2882. u64 logical = (u64)bio->bi_sector << 9;
  2883. u64 length = 0;
  2884. u64 map_length;
  2885. int ret;
  2886. int dev_nr = 0;
  2887. int total_devs = 1;
  2888. struct btrfs_bio *bbio = NULL;
  2889. length = bio->bi_size;
  2890. map_tree = &root->fs_info->mapping_tree;
  2891. map_length = length;
  2892. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  2893. mirror_num);
  2894. BUG_ON(ret);
  2895. total_devs = bbio->num_stripes;
  2896. if (map_length < length) {
  2897. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  2898. "len %llu\n", (unsigned long long)logical,
  2899. (unsigned long long)length,
  2900. (unsigned long long)map_length);
  2901. BUG();
  2902. }
  2903. bbio->orig_bio = first_bio;
  2904. bbio->private = first_bio->bi_private;
  2905. bbio->end_io = first_bio->bi_end_io;
  2906. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  2907. while (dev_nr < total_devs) {
  2908. if (dev_nr < total_devs - 1) {
  2909. bio = bio_clone(first_bio, GFP_NOFS);
  2910. BUG_ON(!bio);
  2911. } else {
  2912. bio = first_bio;
  2913. }
  2914. bio->bi_private = bbio;
  2915. bio->bi_end_io = btrfs_end_bio;
  2916. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  2917. dev = bbio->stripes[dev_nr].dev;
  2918. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  2919. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  2920. "(%s id %llu), size=%u\n", rw,
  2921. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  2922. dev->name, dev->devid, bio->bi_size);
  2923. bio->bi_bdev = dev->bdev;
  2924. if (async_submit)
  2925. schedule_bio(root, dev, rw, bio);
  2926. else
  2927. btrfsic_submit_bio(rw, bio);
  2928. } else {
  2929. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  2930. bio->bi_sector = logical >> 9;
  2931. bio_endio(bio, -EIO);
  2932. }
  2933. dev_nr++;
  2934. }
  2935. return 0;
  2936. }
  2937. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  2938. u8 *uuid, u8 *fsid)
  2939. {
  2940. struct btrfs_device *device;
  2941. struct btrfs_fs_devices *cur_devices;
  2942. cur_devices = root->fs_info->fs_devices;
  2943. while (cur_devices) {
  2944. if (!fsid ||
  2945. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  2946. device = __find_device(&cur_devices->devices,
  2947. devid, uuid);
  2948. if (device)
  2949. return device;
  2950. }
  2951. cur_devices = cur_devices->seed;
  2952. }
  2953. return NULL;
  2954. }
  2955. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  2956. u64 devid, u8 *dev_uuid)
  2957. {
  2958. struct btrfs_device *device;
  2959. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2960. device = kzalloc(sizeof(*device), GFP_NOFS);
  2961. if (!device)
  2962. return NULL;
  2963. list_add(&device->dev_list,
  2964. &fs_devices->devices);
  2965. device->dev_root = root->fs_info->dev_root;
  2966. device->devid = devid;
  2967. device->work.func = pending_bios_fn;
  2968. device->fs_devices = fs_devices;
  2969. device->missing = 1;
  2970. fs_devices->num_devices++;
  2971. fs_devices->missing_devices++;
  2972. spin_lock_init(&device->io_lock);
  2973. INIT_LIST_HEAD(&device->dev_alloc_list);
  2974. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  2975. return device;
  2976. }
  2977. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  2978. struct extent_buffer *leaf,
  2979. struct btrfs_chunk *chunk)
  2980. {
  2981. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2982. struct map_lookup *map;
  2983. struct extent_map *em;
  2984. u64 logical;
  2985. u64 length;
  2986. u64 devid;
  2987. u8 uuid[BTRFS_UUID_SIZE];
  2988. int num_stripes;
  2989. int ret;
  2990. int i;
  2991. logical = key->offset;
  2992. length = btrfs_chunk_length(leaf, chunk);
  2993. read_lock(&map_tree->map_tree.lock);
  2994. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2995. read_unlock(&map_tree->map_tree.lock);
  2996. /* already mapped? */
  2997. if (em && em->start <= logical && em->start + em->len > logical) {
  2998. free_extent_map(em);
  2999. return 0;
  3000. } else if (em) {
  3001. free_extent_map(em);
  3002. }
  3003. em = alloc_extent_map();
  3004. if (!em)
  3005. return -ENOMEM;
  3006. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3007. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3008. if (!map) {
  3009. free_extent_map(em);
  3010. return -ENOMEM;
  3011. }
  3012. em->bdev = (struct block_device *)map;
  3013. em->start = logical;
  3014. em->len = length;
  3015. em->block_start = 0;
  3016. em->block_len = em->len;
  3017. map->num_stripes = num_stripes;
  3018. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3019. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3020. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3021. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3022. map->type = btrfs_chunk_type(leaf, chunk);
  3023. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3024. for (i = 0; i < num_stripes; i++) {
  3025. map->stripes[i].physical =
  3026. btrfs_stripe_offset_nr(leaf, chunk, i);
  3027. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3028. read_extent_buffer(leaf, uuid, (unsigned long)
  3029. btrfs_stripe_dev_uuid_nr(chunk, i),
  3030. BTRFS_UUID_SIZE);
  3031. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3032. NULL);
  3033. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3034. kfree(map);
  3035. free_extent_map(em);
  3036. return -EIO;
  3037. }
  3038. if (!map->stripes[i].dev) {
  3039. map->stripes[i].dev =
  3040. add_missing_dev(root, devid, uuid);
  3041. if (!map->stripes[i].dev) {
  3042. kfree(map);
  3043. free_extent_map(em);
  3044. return -EIO;
  3045. }
  3046. }
  3047. map->stripes[i].dev->in_fs_metadata = 1;
  3048. }
  3049. write_lock(&map_tree->map_tree.lock);
  3050. ret = add_extent_mapping(&map_tree->map_tree, em);
  3051. write_unlock(&map_tree->map_tree.lock);
  3052. BUG_ON(ret);
  3053. free_extent_map(em);
  3054. return 0;
  3055. }
  3056. static int fill_device_from_item(struct extent_buffer *leaf,
  3057. struct btrfs_dev_item *dev_item,
  3058. struct btrfs_device *device)
  3059. {
  3060. unsigned long ptr;
  3061. device->devid = btrfs_device_id(leaf, dev_item);
  3062. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3063. device->total_bytes = device->disk_total_bytes;
  3064. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3065. device->type = btrfs_device_type(leaf, dev_item);
  3066. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3067. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3068. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3069. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3070. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3071. return 0;
  3072. }
  3073. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3074. {
  3075. struct btrfs_fs_devices *fs_devices;
  3076. int ret;
  3077. mutex_lock(&uuid_mutex);
  3078. fs_devices = root->fs_info->fs_devices->seed;
  3079. while (fs_devices) {
  3080. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3081. ret = 0;
  3082. goto out;
  3083. }
  3084. fs_devices = fs_devices->seed;
  3085. }
  3086. fs_devices = find_fsid(fsid);
  3087. if (!fs_devices) {
  3088. ret = -ENOENT;
  3089. goto out;
  3090. }
  3091. fs_devices = clone_fs_devices(fs_devices);
  3092. if (IS_ERR(fs_devices)) {
  3093. ret = PTR_ERR(fs_devices);
  3094. goto out;
  3095. }
  3096. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3097. root->fs_info->bdev_holder);
  3098. if (ret)
  3099. goto out;
  3100. if (!fs_devices->seeding) {
  3101. __btrfs_close_devices(fs_devices);
  3102. free_fs_devices(fs_devices);
  3103. ret = -EINVAL;
  3104. goto out;
  3105. }
  3106. fs_devices->seed = root->fs_info->fs_devices->seed;
  3107. root->fs_info->fs_devices->seed = fs_devices;
  3108. out:
  3109. mutex_unlock(&uuid_mutex);
  3110. return ret;
  3111. }
  3112. static int read_one_dev(struct btrfs_root *root,
  3113. struct extent_buffer *leaf,
  3114. struct btrfs_dev_item *dev_item)
  3115. {
  3116. struct btrfs_device *device;
  3117. u64 devid;
  3118. int ret;
  3119. u8 fs_uuid[BTRFS_UUID_SIZE];
  3120. u8 dev_uuid[BTRFS_UUID_SIZE];
  3121. devid = btrfs_device_id(leaf, dev_item);
  3122. read_extent_buffer(leaf, dev_uuid,
  3123. (unsigned long)btrfs_device_uuid(dev_item),
  3124. BTRFS_UUID_SIZE);
  3125. read_extent_buffer(leaf, fs_uuid,
  3126. (unsigned long)btrfs_device_fsid(dev_item),
  3127. BTRFS_UUID_SIZE);
  3128. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3129. ret = open_seed_devices(root, fs_uuid);
  3130. if (ret && !btrfs_test_opt(root, DEGRADED))
  3131. return ret;
  3132. }
  3133. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3134. if (!device || !device->bdev) {
  3135. if (!btrfs_test_opt(root, DEGRADED))
  3136. return -EIO;
  3137. if (!device) {
  3138. printk(KERN_WARNING "warning devid %llu missing\n",
  3139. (unsigned long long)devid);
  3140. device = add_missing_dev(root, devid, dev_uuid);
  3141. if (!device)
  3142. return -ENOMEM;
  3143. } else if (!device->missing) {
  3144. /*
  3145. * this happens when a device that was properly setup
  3146. * in the device info lists suddenly goes bad.
  3147. * device->bdev is NULL, and so we have to set
  3148. * device->missing to one here
  3149. */
  3150. root->fs_info->fs_devices->missing_devices++;
  3151. device->missing = 1;
  3152. }
  3153. }
  3154. if (device->fs_devices != root->fs_info->fs_devices) {
  3155. BUG_ON(device->writeable);
  3156. if (device->generation !=
  3157. btrfs_device_generation(leaf, dev_item))
  3158. return -EINVAL;
  3159. }
  3160. fill_device_from_item(leaf, dev_item, device);
  3161. device->dev_root = root->fs_info->dev_root;
  3162. device->in_fs_metadata = 1;
  3163. if (device->writeable) {
  3164. device->fs_devices->total_rw_bytes += device->total_bytes;
  3165. spin_lock(&root->fs_info->free_chunk_lock);
  3166. root->fs_info->free_chunk_space += device->total_bytes -
  3167. device->bytes_used;
  3168. spin_unlock(&root->fs_info->free_chunk_lock);
  3169. }
  3170. ret = 0;
  3171. return ret;
  3172. }
  3173. int btrfs_read_sys_array(struct btrfs_root *root)
  3174. {
  3175. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3176. struct extent_buffer *sb;
  3177. struct btrfs_disk_key *disk_key;
  3178. struct btrfs_chunk *chunk;
  3179. u8 *ptr;
  3180. unsigned long sb_ptr;
  3181. int ret = 0;
  3182. u32 num_stripes;
  3183. u32 array_size;
  3184. u32 len = 0;
  3185. u32 cur;
  3186. struct btrfs_key key;
  3187. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3188. BTRFS_SUPER_INFO_SIZE);
  3189. if (!sb)
  3190. return -ENOMEM;
  3191. btrfs_set_buffer_uptodate(sb);
  3192. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3193. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3194. array_size = btrfs_super_sys_array_size(super_copy);
  3195. ptr = super_copy->sys_chunk_array;
  3196. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3197. cur = 0;
  3198. while (cur < array_size) {
  3199. disk_key = (struct btrfs_disk_key *)ptr;
  3200. btrfs_disk_key_to_cpu(&key, disk_key);
  3201. len = sizeof(*disk_key); ptr += len;
  3202. sb_ptr += len;
  3203. cur += len;
  3204. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3205. chunk = (struct btrfs_chunk *)sb_ptr;
  3206. ret = read_one_chunk(root, &key, sb, chunk);
  3207. if (ret)
  3208. break;
  3209. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3210. len = btrfs_chunk_item_size(num_stripes);
  3211. } else {
  3212. ret = -EIO;
  3213. break;
  3214. }
  3215. ptr += len;
  3216. sb_ptr += len;
  3217. cur += len;
  3218. }
  3219. free_extent_buffer(sb);
  3220. return ret;
  3221. }
  3222. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3223. {
  3224. struct btrfs_path *path;
  3225. struct extent_buffer *leaf;
  3226. struct btrfs_key key;
  3227. struct btrfs_key found_key;
  3228. int ret;
  3229. int slot;
  3230. root = root->fs_info->chunk_root;
  3231. path = btrfs_alloc_path();
  3232. if (!path)
  3233. return -ENOMEM;
  3234. /* first we search for all of the device items, and then we
  3235. * read in all of the chunk items. This way we can create chunk
  3236. * mappings that reference all of the devices that are afound
  3237. */
  3238. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3239. key.offset = 0;
  3240. key.type = 0;
  3241. again:
  3242. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3243. if (ret < 0)
  3244. goto error;
  3245. while (1) {
  3246. leaf = path->nodes[0];
  3247. slot = path->slots[0];
  3248. if (slot >= btrfs_header_nritems(leaf)) {
  3249. ret = btrfs_next_leaf(root, path);
  3250. if (ret == 0)
  3251. continue;
  3252. if (ret < 0)
  3253. goto error;
  3254. break;
  3255. }
  3256. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3257. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3258. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3259. break;
  3260. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3261. struct btrfs_dev_item *dev_item;
  3262. dev_item = btrfs_item_ptr(leaf, slot,
  3263. struct btrfs_dev_item);
  3264. ret = read_one_dev(root, leaf, dev_item);
  3265. if (ret)
  3266. goto error;
  3267. }
  3268. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3269. struct btrfs_chunk *chunk;
  3270. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3271. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3272. if (ret)
  3273. goto error;
  3274. }
  3275. path->slots[0]++;
  3276. }
  3277. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3278. key.objectid = 0;
  3279. btrfs_release_path(path);
  3280. goto again;
  3281. }
  3282. ret = 0;
  3283. error:
  3284. btrfs_free_path(path);
  3285. return ret;
  3286. }