volumes.c 84 KB

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