volumes.c 114 KB

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