volumes.c 113 KB

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