volumes.c 113 KB

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