volumes.c 95 KB

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