volumes.c 114 KB

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