volumes.c 115 KB

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