volumes.c 95 KB

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