volumes.c 97 KB

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