volumes.c 79 KB

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