volumes.c 93 KB

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