volumes.c 93 KB

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