volumes.c 93 KB

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