volumes.c 83 KB

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