volumes.c 85 KB

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