volumes.c 83 KB

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