volumes.c 96 KB

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