volumes.c 112 KB

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