volumes.c 84 KB

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