volumes.c 115 KB

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