volumes.c 92 KB

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