volumes.c 112 KB

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