volumes.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397
  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_root *chunk_root = fs_info->chunk_root;
  2081. struct btrfs_root *dev_root = fs_info->dev_root;
  2082. struct list_head *devices;
  2083. struct btrfs_device *device;
  2084. u64 old_size;
  2085. u64 size_to_free;
  2086. struct btrfs_chunk *chunk;
  2087. struct btrfs_path *path;
  2088. struct btrfs_key key;
  2089. struct btrfs_key found_key;
  2090. struct btrfs_trans_handle *trans;
  2091. struct extent_buffer *leaf;
  2092. int slot;
  2093. int ret;
  2094. int enospc_errors = 0;
  2095. /* step one make some room on all the devices */
  2096. devices = &fs_info->fs_devices->devices;
  2097. list_for_each_entry(device, devices, dev_list) {
  2098. old_size = device->total_bytes;
  2099. size_to_free = div_factor(old_size, 1);
  2100. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2101. if (!device->writeable ||
  2102. device->total_bytes - device->bytes_used > size_to_free)
  2103. continue;
  2104. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2105. if (ret == -ENOSPC)
  2106. break;
  2107. BUG_ON(ret);
  2108. trans = btrfs_start_transaction(dev_root, 0);
  2109. BUG_ON(IS_ERR(trans));
  2110. ret = btrfs_grow_device(trans, device, old_size);
  2111. BUG_ON(ret);
  2112. btrfs_end_transaction(trans, dev_root);
  2113. }
  2114. /* step two, relocate all the chunks */
  2115. path = btrfs_alloc_path();
  2116. if (!path) {
  2117. ret = -ENOMEM;
  2118. goto error;
  2119. }
  2120. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2121. key.offset = (u64)-1;
  2122. key.type = BTRFS_CHUNK_ITEM_KEY;
  2123. while (1) {
  2124. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2125. if (ret < 0)
  2126. goto error;
  2127. /*
  2128. * this shouldn't happen, it means the last relocate
  2129. * failed
  2130. */
  2131. if (ret == 0)
  2132. BUG(); /* FIXME break ? */
  2133. ret = btrfs_previous_item(chunk_root, path, 0,
  2134. BTRFS_CHUNK_ITEM_KEY);
  2135. if (ret) {
  2136. ret = 0;
  2137. break;
  2138. }
  2139. leaf = path->nodes[0];
  2140. slot = path->slots[0];
  2141. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2142. if (found_key.objectid != key.objectid)
  2143. break;
  2144. /* chunk zero is special */
  2145. if (found_key.offset == 0)
  2146. break;
  2147. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2148. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2149. found_key.offset);
  2150. btrfs_release_path(path);
  2151. if (!ret)
  2152. goto loop;
  2153. ret = btrfs_relocate_chunk(chunk_root,
  2154. chunk_root->root_key.objectid,
  2155. found_key.objectid,
  2156. found_key.offset);
  2157. if (ret && ret != -ENOSPC)
  2158. goto error;
  2159. if (ret == -ENOSPC)
  2160. enospc_errors++;
  2161. loop:
  2162. key.offset = found_key.offset - 1;
  2163. }
  2164. error:
  2165. btrfs_free_path(path);
  2166. if (enospc_errors) {
  2167. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2168. enospc_errors);
  2169. if (!ret)
  2170. ret = -ENOSPC;
  2171. }
  2172. return ret;
  2173. }
  2174. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2175. {
  2176. int ret;
  2177. unset_balance_control(fs_info);
  2178. ret = del_balance_item(fs_info->tree_root);
  2179. BUG_ON(ret);
  2180. }
  2181. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  2182. struct btrfs_ioctl_balance_args *bargs);
  2183. /*
  2184. * Should be called with both balance and volume mutexes held
  2185. */
  2186. int btrfs_balance(struct btrfs_balance_control *bctl,
  2187. struct btrfs_ioctl_balance_args *bargs)
  2188. {
  2189. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2190. u64 allowed;
  2191. int ret;
  2192. if (btrfs_fs_closing(fs_info)) {
  2193. ret = -EINVAL;
  2194. goto out;
  2195. }
  2196. /*
  2197. * In case of mixed groups both data and meta should be picked,
  2198. * and identical options should be given for both of them.
  2199. */
  2200. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2201. if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2202. (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
  2203. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2204. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2205. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2206. printk(KERN_ERR "btrfs: with mixed groups data and "
  2207. "metadata balance options must be the same\n");
  2208. ret = -EINVAL;
  2209. goto out;
  2210. }
  2211. }
  2212. /*
  2213. * Profile changing sanity checks. Skip them if a simple
  2214. * balance is requested.
  2215. */
  2216. if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
  2217. BTRFS_BALANCE_ARGS_CONVERT))
  2218. goto do_balance;
  2219. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2220. if (fs_info->fs_devices->num_devices == 1)
  2221. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2222. else if (fs_info->fs_devices->num_devices < 4)
  2223. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2224. else
  2225. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2226. BTRFS_BLOCK_GROUP_RAID10);
  2227. if (!profile_is_valid(bctl->data.target, 1) ||
  2228. bctl->data.target & ~allowed) {
  2229. printk(KERN_ERR "btrfs: unable to start balance with target "
  2230. "data profile %llu\n",
  2231. (unsigned long long)bctl->data.target);
  2232. ret = -EINVAL;
  2233. goto out;
  2234. }
  2235. if (!profile_is_valid(bctl->meta.target, 1) ||
  2236. bctl->meta.target & ~allowed) {
  2237. printk(KERN_ERR "btrfs: unable to start balance with target "
  2238. "metadata profile %llu\n",
  2239. (unsigned long long)bctl->meta.target);
  2240. ret = -EINVAL;
  2241. goto out;
  2242. }
  2243. if (!profile_is_valid(bctl->sys.target, 1) ||
  2244. bctl->sys.target & ~allowed) {
  2245. printk(KERN_ERR "btrfs: unable to start balance with target "
  2246. "system profile %llu\n",
  2247. (unsigned long long)bctl->sys.target);
  2248. ret = -EINVAL;
  2249. goto out;
  2250. }
  2251. if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
  2252. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2253. ret = -EINVAL;
  2254. goto out;
  2255. }
  2256. /* allow to reduce meta or sys integrity only if force set */
  2257. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2258. BTRFS_BLOCK_GROUP_RAID10;
  2259. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2260. (fs_info->avail_system_alloc_bits & allowed) &&
  2261. !(bctl->sys.target & allowed)) ||
  2262. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2263. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2264. !(bctl->meta.target & allowed))) {
  2265. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2266. printk(KERN_INFO "btrfs: force reducing metadata "
  2267. "integrity\n");
  2268. } else {
  2269. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2270. "integrity, use force if you want this\n");
  2271. ret = -EINVAL;
  2272. goto out;
  2273. }
  2274. }
  2275. do_balance:
  2276. ret = insert_balance_item(fs_info->tree_root, bctl);
  2277. if (ret && ret != -EEXIST)
  2278. goto out;
  2279. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2280. BUG_ON(ret == -EEXIST);
  2281. set_balance_control(bctl);
  2282. } else {
  2283. BUG_ON(ret != -EEXIST);
  2284. spin_lock(&fs_info->balance_lock);
  2285. update_balance_args(bctl);
  2286. spin_unlock(&fs_info->balance_lock);
  2287. }
  2288. mutex_unlock(&fs_info->balance_mutex);
  2289. ret = __btrfs_balance(fs_info);
  2290. mutex_lock(&fs_info->balance_mutex);
  2291. if (bargs) {
  2292. memset(bargs, 0, sizeof(*bargs));
  2293. update_ioctl_balance_args(fs_info, bargs);
  2294. }
  2295. __cancel_balance(fs_info);
  2296. return ret;
  2297. out:
  2298. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2299. __cancel_balance(fs_info);
  2300. else
  2301. kfree(bctl);
  2302. return ret;
  2303. }
  2304. static int balance_kthread(void *data)
  2305. {
  2306. struct btrfs_balance_control *bctl =
  2307. (struct btrfs_balance_control *)data;
  2308. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2309. int ret = 0;
  2310. mutex_lock(&fs_info->volume_mutex);
  2311. mutex_lock(&fs_info->balance_mutex);
  2312. set_balance_control(bctl);
  2313. if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
  2314. printk(KERN_INFO "btrfs: force skipping balance\n");
  2315. } else {
  2316. printk(KERN_INFO "btrfs: continuing balance\n");
  2317. ret = btrfs_balance(bctl, NULL);
  2318. }
  2319. mutex_unlock(&fs_info->balance_mutex);
  2320. mutex_unlock(&fs_info->volume_mutex);
  2321. return ret;
  2322. }
  2323. int btrfs_recover_balance(struct btrfs_root *tree_root)
  2324. {
  2325. struct task_struct *tsk;
  2326. struct btrfs_balance_control *bctl;
  2327. struct btrfs_balance_item *item;
  2328. struct btrfs_disk_balance_args disk_bargs;
  2329. struct btrfs_path *path;
  2330. struct extent_buffer *leaf;
  2331. struct btrfs_key key;
  2332. int ret;
  2333. path = btrfs_alloc_path();
  2334. if (!path)
  2335. return -ENOMEM;
  2336. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2337. if (!bctl) {
  2338. ret = -ENOMEM;
  2339. goto out;
  2340. }
  2341. key.objectid = BTRFS_BALANCE_OBJECTID;
  2342. key.type = BTRFS_BALANCE_ITEM_KEY;
  2343. key.offset = 0;
  2344. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  2345. if (ret < 0)
  2346. goto out_bctl;
  2347. if (ret > 0) { /* ret = -ENOENT; */
  2348. ret = 0;
  2349. goto out_bctl;
  2350. }
  2351. leaf = path->nodes[0];
  2352. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2353. bctl->fs_info = tree_root->fs_info;
  2354. bctl->flags = btrfs_balance_flags(leaf, item) | BTRFS_BALANCE_RESUME;
  2355. btrfs_balance_data(leaf, item, &disk_bargs);
  2356. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2357. btrfs_balance_meta(leaf, item, &disk_bargs);
  2358. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2359. btrfs_balance_sys(leaf, item, &disk_bargs);
  2360. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2361. tsk = kthread_run(balance_kthread, bctl, "btrfs-balance");
  2362. if (IS_ERR(tsk))
  2363. ret = PTR_ERR(tsk);
  2364. else
  2365. goto out;
  2366. out_bctl:
  2367. kfree(bctl);
  2368. out:
  2369. btrfs_free_path(path);
  2370. return ret;
  2371. }
  2372. /*
  2373. * shrinking a device means finding all of the device extents past
  2374. * the new size, and then following the back refs to the chunks.
  2375. * The chunk relocation code actually frees the device extent
  2376. */
  2377. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2378. {
  2379. struct btrfs_trans_handle *trans;
  2380. struct btrfs_root *root = device->dev_root;
  2381. struct btrfs_dev_extent *dev_extent = NULL;
  2382. struct btrfs_path *path;
  2383. u64 length;
  2384. u64 chunk_tree;
  2385. u64 chunk_objectid;
  2386. u64 chunk_offset;
  2387. int ret;
  2388. int slot;
  2389. int failed = 0;
  2390. bool retried = false;
  2391. struct extent_buffer *l;
  2392. struct btrfs_key key;
  2393. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2394. u64 old_total = btrfs_super_total_bytes(super_copy);
  2395. u64 old_size = device->total_bytes;
  2396. u64 diff = device->total_bytes - new_size;
  2397. if (new_size >= device->total_bytes)
  2398. return -EINVAL;
  2399. path = btrfs_alloc_path();
  2400. if (!path)
  2401. return -ENOMEM;
  2402. path->reada = 2;
  2403. lock_chunks(root);
  2404. device->total_bytes = new_size;
  2405. if (device->writeable) {
  2406. device->fs_devices->total_rw_bytes -= diff;
  2407. spin_lock(&root->fs_info->free_chunk_lock);
  2408. root->fs_info->free_chunk_space -= diff;
  2409. spin_unlock(&root->fs_info->free_chunk_lock);
  2410. }
  2411. unlock_chunks(root);
  2412. again:
  2413. key.objectid = device->devid;
  2414. key.offset = (u64)-1;
  2415. key.type = BTRFS_DEV_EXTENT_KEY;
  2416. while (1) {
  2417. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2418. if (ret < 0)
  2419. goto done;
  2420. ret = btrfs_previous_item(root, path, 0, key.type);
  2421. if (ret < 0)
  2422. goto done;
  2423. if (ret) {
  2424. ret = 0;
  2425. btrfs_release_path(path);
  2426. break;
  2427. }
  2428. l = path->nodes[0];
  2429. slot = path->slots[0];
  2430. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2431. if (key.objectid != device->devid) {
  2432. btrfs_release_path(path);
  2433. break;
  2434. }
  2435. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2436. length = btrfs_dev_extent_length(l, dev_extent);
  2437. if (key.offset + length <= new_size) {
  2438. btrfs_release_path(path);
  2439. break;
  2440. }
  2441. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2442. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2443. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2444. btrfs_release_path(path);
  2445. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2446. chunk_offset);
  2447. if (ret && ret != -ENOSPC)
  2448. goto done;
  2449. if (ret == -ENOSPC)
  2450. failed++;
  2451. key.offset -= 1;
  2452. }
  2453. if (failed && !retried) {
  2454. failed = 0;
  2455. retried = true;
  2456. goto again;
  2457. } else if (failed && retried) {
  2458. ret = -ENOSPC;
  2459. lock_chunks(root);
  2460. device->total_bytes = old_size;
  2461. if (device->writeable)
  2462. device->fs_devices->total_rw_bytes += diff;
  2463. spin_lock(&root->fs_info->free_chunk_lock);
  2464. root->fs_info->free_chunk_space += diff;
  2465. spin_unlock(&root->fs_info->free_chunk_lock);
  2466. unlock_chunks(root);
  2467. goto done;
  2468. }
  2469. /* Shrinking succeeded, else we would be at "done". */
  2470. trans = btrfs_start_transaction(root, 0);
  2471. if (IS_ERR(trans)) {
  2472. ret = PTR_ERR(trans);
  2473. goto done;
  2474. }
  2475. lock_chunks(root);
  2476. device->disk_total_bytes = new_size;
  2477. /* Now btrfs_update_device() will change the on-disk size. */
  2478. ret = btrfs_update_device(trans, device);
  2479. if (ret) {
  2480. unlock_chunks(root);
  2481. btrfs_end_transaction(trans, root);
  2482. goto done;
  2483. }
  2484. WARN_ON(diff > old_total);
  2485. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  2486. unlock_chunks(root);
  2487. btrfs_end_transaction(trans, root);
  2488. done:
  2489. btrfs_free_path(path);
  2490. return ret;
  2491. }
  2492. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  2493. struct btrfs_root *root,
  2494. struct btrfs_key *key,
  2495. struct btrfs_chunk *chunk, int item_size)
  2496. {
  2497. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2498. struct btrfs_disk_key disk_key;
  2499. u32 array_size;
  2500. u8 *ptr;
  2501. array_size = btrfs_super_sys_array_size(super_copy);
  2502. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2503. return -EFBIG;
  2504. ptr = super_copy->sys_chunk_array + array_size;
  2505. btrfs_cpu_key_to_disk(&disk_key, key);
  2506. memcpy(ptr, &disk_key, sizeof(disk_key));
  2507. ptr += sizeof(disk_key);
  2508. memcpy(ptr, chunk, item_size);
  2509. item_size += sizeof(disk_key);
  2510. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2511. return 0;
  2512. }
  2513. /*
  2514. * sort the devices in descending order by max_avail, total_avail
  2515. */
  2516. static int btrfs_cmp_device_info(const void *a, const void *b)
  2517. {
  2518. const struct btrfs_device_info *di_a = a;
  2519. const struct btrfs_device_info *di_b = b;
  2520. if (di_a->max_avail > di_b->max_avail)
  2521. return -1;
  2522. if (di_a->max_avail < di_b->max_avail)
  2523. return 1;
  2524. if (di_a->total_avail > di_b->total_avail)
  2525. return -1;
  2526. if (di_a->total_avail < di_b->total_avail)
  2527. return 1;
  2528. return 0;
  2529. }
  2530. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2531. struct btrfs_root *extent_root,
  2532. struct map_lookup **map_ret,
  2533. u64 *num_bytes_out, u64 *stripe_size_out,
  2534. u64 start, u64 type)
  2535. {
  2536. struct btrfs_fs_info *info = extent_root->fs_info;
  2537. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2538. struct list_head *cur;
  2539. struct map_lookup *map = NULL;
  2540. struct extent_map_tree *em_tree;
  2541. struct extent_map *em;
  2542. struct btrfs_device_info *devices_info = NULL;
  2543. u64 total_avail;
  2544. int num_stripes; /* total number of stripes to allocate */
  2545. int sub_stripes; /* sub_stripes info for map */
  2546. int dev_stripes; /* stripes per dev */
  2547. int devs_max; /* max devs to use */
  2548. int devs_min; /* min devs needed */
  2549. int devs_increment; /* ndevs has to be a multiple of this */
  2550. int ncopies; /* how many copies to data has */
  2551. int ret;
  2552. u64 max_stripe_size;
  2553. u64 max_chunk_size;
  2554. u64 stripe_size;
  2555. u64 num_bytes;
  2556. int ndevs;
  2557. int i;
  2558. int j;
  2559. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2560. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2561. WARN_ON(1);
  2562. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2563. }
  2564. if (list_empty(&fs_devices->alloc_list))
  2565. return -ENOSPC;
  2566. sub_stripes = 1;
  2567. dev_stripes = 1;
  2568. devs_increment = 1;
  2569. ncopies = 1;
  2570. devs_max = 0; /* 0 == as many as possible */
  2571. devs_min = 1;
  2572. /*
  2573. * define the properties of each RAID type.
  2574. * FIXME: move this to a global table and use it in all RAID
  2575. * calculation code
  2576. */
  2577. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2578. dev_stripes = 2;
  2579. ncopies = 2;
  2580. devs_max = 1;
  2581. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2582. devs_min = 2;
  2583. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2584. devs_increment = 2;
  2585. ncopies = 2;
  2586. devs_max = 2;
  2587. devs_min = 2;
  2588. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2589. sub_stripes = 2;
  2590. devs_increment = 2;
  2591. ncopies = 2;
  2592. devs_min = 4;
  2593. } else {
  2594. devs_max = 1;
  2595. }
  2596. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2597. max_stripe_size = 1024 * 1024 * 1024;
  2598. max_chunk_size = 10 * max_stripe_size;
  2599. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2600. max_stripe_size = 256 * 1024 * 1024;
  2601. max_chunk_size = max_stripe_size;
  2602. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2603. max_stripe_size = 8 * 1024 * 1024;
  2604. max_chunk_size = 2 * max_stripe_size;
  2605. } else {
  2606. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2607. type);
  2608. BUG_ON(1);
  2609. }
  2610. /* we don't want a chunk larger than 10% of writeable space */
  2611. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2612. max_chunk_size);
  2613. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2614. GFP_NOFS);
  2615. if (!devices_info)
  2616. return -ENOMEM;
  2617. cur = fs_devices->alloc_list.next;
  2618. /*
  2619. * in the first pass through the devices list, we gather information
  2620. * about the available holes on each device.
  2621. */
  2622. ndevs = 0;
  2623. while (cur != &fs_devices->alloc_list) {
  2624. struct btrfs_device *device;
  2625. u64 max_avail;
  2626. u64 dev_offset;
  2627. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2628. cur = cur->next;
  2629. if (!device->writeable) {
  2630. printk(KERN_ERR
  2631. "btrfs: read-only device in alloc_list\n");
  2632. WARN_ON(1);
  2633. continue;
  2634. }
  2635. if (!device->in_fs_metadata)
  2636. continue;
  2637. if (device->total_bytes > device->bytes_used)
  2638. total_avail = device->total_bytes - device->bytes_used;
  2639. else
  2640. total_avail = 0;
  2641. /* If there is no space on this device, skip it. */
  2642. if (total_avail == 0)
  2643. continue;
  2644. ret = find_free_dev_extent(trans, device,
  2645. max_stripe_size * dev_stripes,
  2646. &dev_offset, &max_avail);
  2647. if (ret && ret != -ENOSPC)
  2648. goto error;
  2649. if (ret == 0)
  2650. max_avail = max_stripe_size * dev_stripes;
  2651. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2652. continue;
  2653. devices_info[ndevs].dev_offset = dev_offset;
  2654. devices_info[ndevs].max_avail = max_avail;
  2655. devices_info[ndevs].total_avail = total_avail;
  2656. devices_info[ndevs].dev = device;
  2657. ++ndevs;
  2658. }
  2659. /*
  2660. * now sort the devices by hole size / available space
  2661. */
  2662. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2663. btrfs_cmp_device_info, NULL);
  2664. /* round down to number of usable stripes */
  2665. ndevs -= ndevs % devs_increment;
  2666. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2667. ret = -ENOSPC;
  2668. goto error;
  2669. }
  2670. if (devs_max && ndevs > devs_max)
  2671. ndevs = devs_max;
  2672. /*
  2673. * the primary goal is to maximize the number of stripes, so use as many
  2674. * devices as possible, even if the stripes are not maximum sized.
  2675. */
  2676. stripe_size = devices_info[ndevs-1].max_avail;
  2677. num_stripes = ndevs * dev_stripes;
  2678. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2679. stripe_size = max_chunk_size * ncopies;
  2680. do_div(stripe_size, num_stripes);
  2681. }
  2682. do_div(stripe_size, dev_stripes);
  2683. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2684. stripe_size *= BTRFS_STRIPE_LEN;
  2685. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2686. if (!map) {
  2687. ret = -ENOMEM;
  2688. goto error;
  2689. }
  2690. map->num_stripes = num_stripes;
  2691. for (i = 0; i < ndevs; ++i) {
  2692. for (j = 0; j < dev_stripes; ++j) {
  2693. int s = i * dev_stripes + j;
  2694. map->stripes[s].dev = devices_info[i].dev;
  2695. map->stripes[s].physical = devices_info[i].dev_offset +
  2696. j * stripe_size;
  2697. }
  2698. }
  2699. map->sector_size = extent_root->sectorsize;
  2700. map->stripe_len = BTRFS_STRIPE_LEN;
  2701. map->io_align = BTRFS_STRIPE_LEN;
  2702. map->io_width = BTRFS_STRIPE_LEN;
  2703. map->type = type;
  2704. map->sub_stripes = sub_stripes;
  2705. *map_ret = map;
  2706. num_bytes = stripe_size * (num_stripes / ncopies);
  2707. *stripe_size_out = stripe_size;
  2708. *num_bytes_out = num_bytes;
  2709. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2710. em = alloc_extent_map();
  2711. if (!em) {
  2712. ret = -ENOMEM;
  2713. goto error;
  2714. }
  2715. em->bdev = (struct block_device *)map;
  2716. em->start = start;
  2717. em->len = num_bytes;
  2718. em->block_start = 0;
  2719. em->block_len = em->len;
  2720. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2721. write_lock(&em_tree->lock);
  2722. ret = add_extent_mapping(em_tree, em);
  2723. write_unlock(&em_tree->lock);
  2724. BUG_ON(ret);
  2725. free_extent_map(em);
  2726. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2727. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2728. start, num_bytes);
  2729. BUG_ON(ret);
  2730. for (i = 0; i < map->num_stripes; ++i) {
  2731. struct btrfs_device *device;
  2732. u64 dev_offset;
  2733. device = map->stripes[i].dev;
  2734. dev_offset = map->stripes[i].physical;
  2735. ret = btrfs_alloc_dev_extent(trans, device,
  2736. info->chunk_root->root_key.objectid,
  2737. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2738. start, dev_offset, stripe_size);
  2739. BUG_ON(ret);
  2740. }
  2741. kfree(devices_info);
  2742. return 0;
  2743. error:
  2744. kfree(map);
  2745. kfree(devices_info);
  2746. return ret;
  2747. }
  2748. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2749. struct btrfs_root *extent_root,
  2750. struct map_lookup *map, u64 chunk_offset,
  2751. u64 chunk_size, u64 stripe_size)
  2752. {
  2753. u64 dev_offset;
  2754. struct btrfs_key key;
  2755. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2756. struct btrfs_device *device;
  2757. struct btrfs_chunk *chunk;
  2758. struct btrfs_stripe *stripe;
  2759. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2760. int index = 0;
  2761. int ret;
  2762. chunk = kzalloc(item_size, GFP_NOFS);
  2763. if (!chunk)
  2764. return -ENOMEM;
  2765. index = 0;
  2766. while (index < map->num_stripes) {
  2767. device = map->stripes[index].dev;
  2768. device->bytes_used += stripe_size;
  2769. ret = btrfs_update_device(trans, device);
  2770. BUG_ON(ret);
  2771. index++;
  2772. }
  2773. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2774. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2775. map->num_stripes);
  2776. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2777. index = 0;
  2778. stripe = &chunk->stripe;
  2779. while (index < map->num_stripes) {
  2780. device = map->stripes[index].dev;
  2781. dev_offset = map->stripes[index].physical;
  2782. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2783. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2784. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2785. stripe++;
  2786. index++;
  2787. }
  2788. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2789. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2790. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2791. btrfs_set_stack_chunk_type(chunk, map->type);
  2792. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2793. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2794. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2795. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2796. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2797. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2798. key.type = BTRFS_CHUNK_ITEM_KEY;
  2799. key.offset = chunk_offset;
  2800. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2801. BUG_ON(ret);
  2802. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2803. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2804. item_size);
  2805. BUG_ON(ret);
  2806. }
  2807. kfree(chunk);
  2808. return 0;
  2809. }
  2810. /*
  2811. * Chunk allocation falls into two parts. The first part does works
  2812. * that make the new allocated chunk useable, but not do any operation
  2813. * that modifies the chunk tree. The second part does the works that
  2814. * require modifying the chunk tree. This division is important for the
  2815. * bootstrap process of adding storage to a seed btrfs.
  2816. */
  2817. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2818. struct btrfs_root *extent_root, u64 type)
  2819. {
  2820. u64 chunk_offset;
  2821. u64 chunk_size;
  2822. u64 stripe_size;
  2823. struct map_lookup *map;
  2824. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2825. int ret;
  2826. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2827. &chunk_offset);
  2828. if (ret)
  2829. return ret;
  2830. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2831. &stripe_size, chunk_offset, type);
  2832. if (ret)
  2833. return ret;
  2834. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2835. chunk_size, stripe_size);
  2836. BUG_ON(ret);
  2837. return 0;
  2838. }
  2839. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2840. struct btrfs_root *root,
  2841. struct btrfs_device *device)
  2842. {
  2843. u64 chunk_offset;
  2844. u64 sys_chunk_offset;
  2845. u64 chunk_size;
  2846. u64 sys_chunk_size;
  2847. u64 stripe_size;
  2848. u64 sys_stripe_size;
  2849. u64 alloc_profile;
  2850. struct map_lookup *map;
  2851. struct map_lookup *sys_map;
  2852. struct btrfs_fs_info *fs_info = root->fs_info;
  2853. struct btrfs_root *extent_root = fs_info->extent_root;
  2854. int ret;
  2855. ret = find_next_chunk(fs_info->chunk_root,
  2856. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2857. if (ret)
  2858. return ret;
  2859. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2860. fs_info->avail_metadata_alloc_bits;
  2861. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2862. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2863. &stripe_size, chunk_offset, alloc_profile);
  2864. BUG_ON(ret);
  2865. sys_chunk_offset = chunk_offset + chunk_size;
  2866. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2867. fs_info->avail_system_alloc_bits;
  2868. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2869. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2870. &sys_chunk_size, &sys_stripe_size,
  2871. sys_chunk_offset, alloc_profile);
  2872. BUG_ON(ret);
  2873. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2874. BUG_ON(ret);
  2875. /*
  2876. * Modifying chunk tree needs allocating new blocks from both
  2877. * system block group and metadata block group. So we only can
  2878. * do operations require modifying the chunk tree after both
  2879. * block groups were created.
  2880. */
  2881. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2882. chunk_size, stripe_size);
  2883. BUG_ON(ret);
  2884. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2885. sys_chunk_offset, sys_chunk_size,
  2886. sys_stripe_size);
  2887. BUG_ON(ret);
  2888. return 0;
  2889. }
  2890. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2891. {
  2892. struct extent_map *em;
  2893. struct map_lookup *map;
  2894. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2895. int readonly = 0;
  2896. int i;
  2897. read_lock(&map_tree->map_tree.lock);
  2898. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2899. read_unlock(&map_tree->map_tree.lock);
  2900. if (!em)
  2901. return 1;
  2902. if (btrfs_test_opt(root, DEGRADED)) {
  2903. free_extent_map(em);
  2904. return 0;
  2905. }
  2906. map = (struct map_lookup *)em->bdev;
  2907. for (i = 0; i < map->num_stripes; i++) {
  2908. if (!map->stripes[i].dev->writeable) {
  2909. readonly = 1;
  2910. break;
  2911. }
  2912. }
  2913. free_extent_map(em);
  2914. return readonly;
  2915. }
  2916. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2917. {
  2918. extent_map_tree_init(&tree->map_tree);
  2919. }
  2920. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2921. {
  2922. struct extent_map *em;
  2923. while (1) {
  2924. write_lock(&tree->map_tree.lock);
  2925. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2926. if (em)
  2927. remove_extent_mapping(&tree->map_tree, em);
  2928. write_unlock(&tree->map_tree.lock);
  2929. if (!em)
  2930. break;
  2931. kfree(em->bdev);
  2932. /* once for us */
  2933. free_extent_map(em);
  2934. /* once for the tree */
  2935. free_extent_map(em);
  2936. }
  2937. }
  2938. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2939. {
  2940. struct extent_map *em;
  2941. struct map_lookup *map;
  2942. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2943. int ret;
  2944. read_lock(&em_tree->lock);
  2945. em = lookup_extent_mapping(em_tree, logical, len);
  2946. read_unlock(&em_tree->lock);
  2947. BUG_ON(!em);
  2948. BUG_ON(em->start > logical || em->start + em->len < logical);
  2949. map = (struct map_lookup *)em->bdev;
  2950. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2951. ret = map->num_stripes;
  2952. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2953. ret = map->sub_stripes;
  2954. else
  2955. ret = 1;
  2956. free_extent_map(em);
  2957. return ret;
  2958. }
  2959. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2960. int optimal)
  2961. {
  2962. int i;
  2963. if (map->stripes[optimal].dev->bdev)
  2964. return optimal;
  2965. for (i = first; i < first + num; i++) {
  2966. if (map->stripes[i].dev->bdev)
  2967. return i;
  2968. }
  2969. /* we couldn't find one that doesn't fail. Just return something
  2970. * and the io error handling code will clean up eventually
  2971. */
  2972. return optimal;
  2973. }
  2974. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2975. u64 logical, u64 *length,
  2976. struct btrfs_bio **bbio_ret,
  2977. int mirror_num)
  2978. {
  2979. struct extent_map *em;
  2980. struct map_lookup *map;
  2981. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2982. u64 offset;
  2983. u64 stripe_offset;
  2984. u64 stripe_end_offset;
  2985. u64 stripe_nr;
  2986. u64 stripe_nr_orig;
  2987. u64 stripe_nr_end;
  2988. int stripes_allocated = 8;
  2989. int stripes_required = 1;
  2990. int stripe_index;
  2991. int i;
  2992. int num_stripes;
  2993. int max_errors = 0;
  2994. struct btrfs_bio *bbio = NULL;
  2995. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2996. stripes_allocated = 1;
  2997. again:
  2998. if (bbio_ret) {
  2999. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  3000. GFP_NOFS);
  3001. if (!bbio)
  3002. return -ENOMEM;
  3003. atomic_set(&bbio->error, 0);
  3004. }
  3005. read_lock(&em_tree->lock);
  3006. em = lookup_extent_mapping(em_tree, logical, *length);
  3007. read_unlock(&em_tree->lock);
  3008. if (!em) {
  3009. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  3010. (unsigned long long)logical,
  3011. (unsigned long long)*length);
  3012. BUG();
  3013. }
  3014. BUG_ON(em->start > logical || em->start + em->len < logical);
  3015. map = (struct map_lookup *)em->bdev;
  3016. offset = logical - em->start;
  3017. if (mirror_num > map->num_stripes)
  3018. mirror_num = 0;
  3019. /* if our btrfs_bio struct is too small, back off and try again */
  3020. if (rw & REQ_WRITE) {
  3021. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  3022. BTRFS_BLOCK_GROUP_DUP)) {
  3023. stripes_required = map->num_stripes;
  3024. max_errors = 1;
  3025. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3026. stripes_required = map->sub_stripes;
  3027. max_errors = 1;
  3028. }
  3029. }
  3030. if (rw & REQ_DISCARD) {
  3031. if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK)
  3032. stripes_required = map->num_stripes;
  3033. }
  3034. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  3035. stripes_allocated < stripes_required) {
  3036. stripes_allocated = map->num_stripes;
  3037. free_extent_map(em);
  3038. kfree(bbio);
  3039. goto again;
  3040. }
  3041. stripe_nr = offset;
  3042. /*
  3043. * stripe_nr counts the total number of stripes we have to stride
  3044. * to get to this block
  3045. */
  3046. do_div(stripe_nr, map->stripe_len);
  3047. stripe_offset = stripe_nr * map->stripe_len;
  3048. BUG_ON(offset < stripe_offset);
  3049. /* stripe_offset is the offset of this block in its stripe*/
  3050. stripe_offset = offset - stripe_offset;
  3051. if (rw & REQ_DISCARD)
  3052. *length = min_t(u64, em->len - offset, *length);
  3053. else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  3054. /* we limit the length of each bio to what fits in a stripe */
  3055. *length = min_t(u64, em->len - offset,
  3056. map->stripe_len - stripe_offset);
  3057. } else {
  3058. *length = em->len - offset;
  3059. }
  3060. if (!bbio_ret)
  3061. goto out;
  3062. num_stripes = 1;
  3063. stripe_index = 0;
  3064. stripe_nr_orig = stripe_nr;
  3065. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  3066. (~(map->stripe_len - 1));
  3067. do_div(stripe_nr_end, map->stripe_len);
  3068. stripe_end_offset = stripe_nr_end * map->stripe_len -
  3069. (offset + *length);
  3070. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3071. if (rw & REQ_DISCARD)
  3072. num_stripes = min_t(u64, map->num_stripes,
  3073. stripe_nr_end - stripe_nr_orig);
  3074. stripe_index = do_div(stripe_nr, map->num_stripes);
  3075. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  3076. if (rw & (REQ_WRITE | REQ_DISCARD))
  3077. num_stripes = map->num_stripes;
  3078. else if (mirror_num)
  3079. stripe_index = mirror_num - 1;
  3080. else {
  3081. stripe_index = find_live_mirror(map, 0,
  3082. map->num_stripes,
  3083. current->pid % map->num_stripes);
  3084. mirror_num = stripe_index + 1;
  3085. }
  3086. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  3087. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  3088. num_stripes = map->num_stripes;
  3089. } else if (mirror_num) {
  3090. stripe_index = mirror_num - 1;
  3091. } else {
  3092. mirror_num = 1;
  3093. }
  3094. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3095. int factor = map->num_stripes / map->sub_stripes;
  3096. stripe_index = do_div(stripe_nr, factor);
  3097. stripe_index *= map->sub_stripes;
  3098. if (rw & REQ_WRITE)
  3099. num_stripes = map->sub_stripes;
  3100. else if (rw & REQ_DISCARD)
  3101. num_stripes = min_t(u64, map->sub_stripes *
  3102. (stripe_nr_end - stripe_nr_orig),
  3103. map->num_stripes);
  3104. else if (mirror_num)
  3105. stripe_index += mirror_num - 1;
  3106. else {
  3107. stripe_index = find_live_mirror(map, stripe_index,
  3108. map->sub_stripes, stripe_index +
  3109. current->pid % map->sub_stripes);
  3110. mirror_num = stripe_index + 1;
  3111. }
  3112. } else {
  3113. /*
  3114. * after this do_div call, stripe_nr is the number of stripes
  3115. * on this device we have to walk to find the data, and
  3116. * stripe_index is the number of our device in the stripe array
  3117. */
  3118. stripe_index = do_div(stripe_nr, map->num_stripes);
  3119. mirror_num = stripe_index + 1;
  3120. }
  3121. BUG_ON(stripe_index >= map->num_stripes);
  3122. if (rw & REQ_DISCARD) {
  3123. for (i = 0; i < num_stripes; i++) {
  3124. bbio->stripes[i].physical =
  3125. map->stripes[stripe_index].physical +
  3126. stripe_offset + stripe_nr * map->stripe_len;
  3127. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  3128. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3129. u64 stripes;
  3130. u32 last_stripe = 0;
  3131. int j;
  3132. div_u64_rem(stripe_nr_end - 1,
  3133. map->num_stripes,
  3134. &last_stripe);
  3135. for (j = 0; j < map->num_stripes; j++) {
  3136. u32 test;
  3137. div_u64_rem(stripe_nr_end - 1 - j,
  3138. map->num_stripes, &test);
  3139. if (test == stripe_index)
  3140. break;
  3141. }
  3142. stripes = stripe_nr_end - 1 - j;
  3143. do_div(stripes, map->num_stripes);
  3144. bbio->stripes[i].length = map->stripe_len *
  3145. (stripes - stripe_nr + 1);
  3146. if (i == 0) {
  3147. bbio->stripes[i].length -=
  3148. stripe_offset;
  3149. stripe_offset = 0;
  3150. }
  3151. if (stripe_index == last_stripe)
  3152. bbio->stripes[i].length -=
  3153. stripe_end_offset;
  3154. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3155. u64 stripes;
  3156. int j;
  3157. int factor = map->num_stripes /
  3158. map->sub_stripes;
  3159. u32 last_stripe = 0;
  3160. div_u64_rem(stripe_nr_end - 1,
  3161. factor, &last_stripe);
  3162. last_stripe *= map->sub_stripes;
  3163. for (j = 0; j < factor; j++) {
  3164. u32 test;
  3165. div_u64_rem(stripe_nr_end - 1 - j,
  3166. factor, &test);
  3167. if (test ==
  3168. stripe_index / map->sub_stripes)
  3169. break;
  3170. }
  3171. stripes = stripe_nr_end - 1 - j;
  3172. do_div(stripes, factor);
  3173. bbio->stripes[i].length = map->stripe_len *
  3174. (stripes - stripe_nr + 1);
  3175. if (i < map->sub_stripes) {
  3176. bbio->stripes[i].length -=
  3177. stripe_offset;
  3178. if (i == map->sub_stripes - 1)
  3179. stripe_offset = 0;
  3180. }
  3181. if (stripe_index >= last_stripe &&
  3182. stripe_index <= (last_stripe +
  3183. map->sub_stripes - 1)) {
  3184. bbio->stripes[i].length -=
  3185. stripe_end_offset;
  3186. }
  3187. } else
  3188. bbio->stripes[i].length = *length;
  3189. stripe_index++;
  3190. if (stripe_index == map->num_stripes) {
  3191. /* This could only happen for RAID0/10 */
  3192. stripe_index = 0;
  3193. stripe_nr++;
  3194. }
  3195. }
  3196. } else {
  3197. for (i = 0; i < num_stripes; i++) {
  3198. bbio->stripes[i].physical =
  3199. map->stripes[stripe_index].physical +
  3200. stripe_offset +
  3201. stripe_nr * map->stripe_len;
  3202. bbio->stripes[i].dev =
  3203. map->stripes[stripe_index].dev;
  3204. stripe_index++;
  3205. }
  3206. }
  3207. if (bbio_ret) {
  3208. *bbio_ret = bbio;
  3209. bbio->num_stripes = num_stripes;
  3210. bbio->max_errors = max_errors;
  3211. bbio->mirror_num = mirror_num;
  3212. }
  3213. out:
  3214. free_extent_map(em);
  3215. return 0;
  3216. }
  3217. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  3218. u64 logical, u64 *length,
  3219. struct btrfs_bio **bbio_ret, int mirror_num)
  3220. {
  3221. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  3222. mirror_num);
  3223. }
  3224. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  3225. u64 chunk_start, u64 physical, u64 devid,
  3226. u64 **logical, int *naddrs, int *stripe_len)
  3227. {
  3228. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3229. struct extent_map *em;
  3230. struct map_lookup *map;
  3231. u64 *buf;
  3232. u64 bytenr;
  3233. u64 length;
  3234. u64 stripe_nr;
  3235. int i, j, nr = 0;
  3236. read_lock(&em_tree->lock);
  3237. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  3238. read_unlock(&em_tree->lock);
  3239. BUG_ON(!em || em->start != chunk_start);
  3240. map = (struct map_lookup *)em->bdev;
  3241. length = em->len;
  3242. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3243. do_div(length, map->num_stripes / map->sub_stripes);
  3244. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3245. do_div(length, map->num_stripes);
  3246. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  3247. BUG_ON(!buf);
  3248. for (i = 0; i < map->num_stripes; i++) {
  3249. if (devid && map->stripes[i].dev->devid != devid)
  3250. continue;
  3251. if (map->stripes[i].physical > physical ||
  3252. map->stripes[i].physical + length <= physical)
  3253. continue;
  3254. stripe_nr = physical - map->stripes[i].physical;
  3255. do_div(stripe_nr, map->stripe_len);
  3256. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3257. stripe_nr = stripe_nr * map->num_stripes + i;
  3258. do_div(stripe_nr, map->sub_stripes);
  3259. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3260. stripe_nr = stripe_nr * map->num_stripes + i;
  3261. }
  3262. bytenr = chunk_start + stripe_nr * map->stripe_len;
  3263. WARN_ON(nr >= map->num_stripes);
  3264. for (j = 0; j < nr; j++) {
  3265. if (buf[j] == bytenr)
  3266. break;
  3267. }
  3268. if (j == nr) {
  3269. WARN_ON(nr >= map->num_stripes);
  3270. buf[nr++] = bytenr;
  3271. }
  3272. }
  3273. *logical = buf;
  3274. *naddrs = nr;
  3275. *stripe_len = map->stripe_len;
  3276. free_extent_map(em);
  3277. return 0;
  3278. }
  3279. static void btrfs_end_bio(struct bio *bio, int err)
  3280. {
  3281. struct btrfs_bio *bbio = bio->bi_private;
  3282. int is_orig_bio = 0;
  3283. if (err)
  3284. atomic_inc(&bbio->error);
  3285. if (bio == bbio->orig_bio)
  3286. is_orig_bio = 1;
  3287. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3288. if (!is_orig_bio) {
  3289. bio_put(bio);
  3290. bio = bbio->orig_bio;
  3291. }
  3292. bio->bi_private = bbio->private;
  3293. bio->bi_end_io = bbio->end_io;
  3294. bio->bi_bdev = (struct block_device *)
  3295. (unsigned long)bbio->mirror_num;
  3296. /* only send an error to the higher layers if it is
  3297. * beyond the tolerance of the multi-bio
  3298. */
  3299. if (atomic_read(&bbio->error) > bbio->max_errors) {
  3300. err = -EIO;
  3301. } else {
  3302. /*
  3303. * this bio is actually up to date, we didn't
  3304. * go over the max number of errors
  3305. */
  3306. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3307. err = 0;
  3308. }
  3309. kfree(bbio);
  3310. bio_endio(bio, err);
  3311. } else if (!is_orig_bio) {
  3312. bio_put(bio);
  3313. }
  3314. }
  3315. struct async_sched {
  3316. struct bio *bio;
  3317. int rw;
  3318. struct btrfs_fs_info *info;
  3319. struct btrfs_work work;
  3320. };
  3321. /*
  3322. * see run_scheduled_bios for a description of why bios are collected for
  3323. * async submit.
  3324. *
  3325. * This will add one bio to the pending list for a device and make sure
  3326. * the work struct is scheduled.
  3327. */
  3328. static noinline int schedule_bio(struct btrfs_root *root,
  3329. struct btrfs_device *device,
  3330. int rw, struct bio *bio)
  3331. {
  3332. int should_queue = 1;
  3333. struct btrfs_pending_bios *pending_bios;
  3334. /* don't bother with additional async steps for reads, right now */
  3335. if (!(rw & REQ_WRITE)) {
  3336. bio_get(bio);
  3337. submit_bio(rw, bio);
  3338. bio_put(bio);
  3339. return 0;
  3340. }
  3341. /*
  3342. * nr_async_bios allows us to reliably return congestion to the
  3343. * higher layers. Otherwise, the async bio makes it appear we have
  3344. * made progress against dirty pages when we've really just put it
  3345. * on a queue for later
  3346. */
  3347. atomic_inc(&root->fs_info->nr_async_bios);
  3348. WARN_ON(bio->bi_next);
  3349. bio->bi_next = NULL;
  3350. bio->bi_rw |= rw;
  3351. spin_lock(&device->io_lock);
  3352. if (bio->bi_rw & REQ_SYNC)
  3353. pending_bios = &device->pending_sync_bios;
  3354. else
  3355. pending_bios = &device->pending_bios;
  3356. if (pending_bios->tail)
  3357. pending_bios->tail->bi_next = bio;
  3358. pending_bios->tail = bio;
  3359. if (!pending_bios->head)
  3360. pending_bios->head = bio;
  3361. if (device->running_pending)
  3362. should_queue = 0;
  3363. spin_unlock(&device->io_lock);
  3364. if (should_queue)
  3365. btrfs_queue_worker(&root->fs_info->submit_workers,
  3366. &device->work);
  3367. return 0;
  3368. }
  3369. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  3370. int mirror_num, int async_submit)
  3371. {
  3372. struct btrfs_mapping_tree *map_tree;
  3373. struct btrfs_device *dev;
  3374. struct bio *first_bio = bio;
  3375. u64 logical = (u64)bio->bi_sector << 9;
  3376. u64 length = 0;
  3377. u64 map_length;
  3378. int ret;
  3379. int dev_nr = 0;
  3380. int total_devs = 1;
  3381. struct btrfs_bio *bbio = NULL;
  3382. length = bio->bi_size;
  3383. map_tree = &root->fs_info->mapping_tree;
  3384. map_length = length;
  3385. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  3386. mirror_num);
  3387. BUG_ON(ret);
  3388. total_devs = bbio->num_stripes;
  3389. if (map_length < length) {
  3390. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  3391. "len %llu\n", (unsigned long long)logical,
  3392. (unsigned long long)length,
  3393. (unsigned long long)map_length);
  3394. BUG();
  3395. }
  3396. bbio->orig_bio = first_bio;
  3397. bbio->private = first_bio->bi_private;
  3398. bbio->end_io = first_bio->bi_end_io;
  3399. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  3400. while (dev_nr < total_devs) {
  3401. if (dev_nr < total_devs - 1) {
  3402. bio = bio_clone(first_bio, GFP_NOFS);
  3403. BUG_ON(!bio);
  3404. } else {
  3405. bio = first_bio;
  3406. }
  3407. bio->bi_private = bbio;
  3408. bio->bi_end_io = btrfs_end_bio;
  3409. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  3410. dev = bbio->stripes[dev_nr].dev;
  3411. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  3412. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  3413. "(%s id %llu), size=%u\n", rw,
  3414. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  3415. dev->name, dev->devid, bio->bi_size);
  3416. bio->bi_bdev = dev->bdev;
  3417. if (async_submit)
  3418. schedule_bio(root, dev, rw, bio);
  3419. else
  3420. submit_bio(rw, bio);
  3421. } else {
  3422. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  3423. bio->bi_sector = logical >> 9;
  3424. bio_endio(bio, -EIO);
  3425. }
  3426. dev_nr++;
  3427. }
  3428. return 0;
  3429. }
  3430. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  3431. u8 *uuid, u8 *fsid)
  3432. {
  3433. struct btrfs_device *device;
  3434. struct btrfs_fs_devices *cur_devices;
  3435. cur_devices = root->fs_info->fs_devices;
  3436. while (cur_devices) {
  3437. if (!fsid ||
  3438. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3439. device = __find_device(&cur_devices->devices,
  3440. devid, uuid);
  3441. if (device)
  3442. return device;
  3443. }
  3444. cur_devices = cur_devices->seed;
  3445. }
  3446. return NULL;
  3447. }
  3448. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  3449. u64 devid, u8 *dev_uuid)
  3450. {
  3451. struct btrfs_device *device;
  3452. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  3453. device = kzalloc(sizeof(*device), GFP_NOFS);
  3454. if (!device)
  3455. return NULL;
  3456. list_add(&device->dev_list,
  3457. &fs_devices->devices);
  3458. device->dev_root = root->fs_info->dev_root;
  3459. device->devid = devid;
  3460. device->work.func = pending_bios_fn;
  3461. device->fs_devices = fs_devices;
  3462. device->missing = 1;
  3463. fs_devices->num_devices++;
  3464. fs_devices->missing_devices++;
  3465. spin_lock_init(&device->io_lock);
  3466. INIT_LIST_HEAD(&device->dev_alloc_list);
  3467. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  3468. return device;
  3469. }
  3470. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  3471. struct extent_buffer *leaf,
  3472. struct btrfs_chunk *chunk)
  3473. {
  3474. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3475. struct map_lookup *map;
  3476. struct extent_map *em;
  3477. u64 logical;
  3478. u64 length;
  3479. u64 devid;
  3480. u8 uuid[BTRFS_UUID_SIZE];
  3481. int num_stripes;
  3482. int ret;
  3483. int i;
  3484. logical = key->offset;
  3485. length = btrfs_chunk_length(leaf, chunk);
  3486. read_lock(&map_tree->map_tree.lock);
  3487. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  3488. read_unlock(&map_tree->map_tree.lock);
  3489. /* already mapped? */
  3490. if (em && em->start <= logical && em->start + em->len > logical) {
  3491. free_extent_map(em);
  3492. return 0;
  3493. } else if (em) {
  3494. free_extent_map(em);
  3495. }
  3496. em = alloc_extent_map();
  3497. if (!em)
  3498. return -ENOMEM;
  3499. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3500. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3501. if (!map) {
  3502. free_extent_map(em);
  3503. return -ENOMEM;
  3504. }
  3505. em->bdev = (struct block_device *)map;
  3506. em->start = logical;
  3507. em->len = length;
  3508. em->block_start = 0;
  3509. em->block_len = em->len;
  3510. map->num_stripes = num_stripes;
  3511. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3512. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3513. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3514. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3515. map->type = btrfs_chunk_type(leaf, chunk);
  3516. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3517. for (i = 0; i < num_stripes; i++) {
  3518. map->stripes[i].physical =
  3519. btrfs_stripe_offset_nr(leaf, chunk, i);
  3520. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3521. read_extent_buffer(leaf, uuid, (unsigned long)
  3522. btrfs_stripe_dev_uuid_nr(chunk, i),
  3523. BTRFS_UUID_SIZE);
  3524. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3525. NULL);
  3526. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3527. kfree(map);
  3528. free_extent_map(em);
  3529. return -EIO;
  3530. }
  3531. if (!map->stripes[i].dev) {
  3532. map->stripes[i].dev =
  3533. add_missing_dev(root, devid, uuid);
  3534. if (!map->stripes[i].dev) {
  3535. kfree(map);
  3536. free_extent_map(em);
  3537. return -EIO;
  3538. }
  3539. }
  3540. map->stripes[i].dev->in_fs_metadata = 1;
  3541. }
  3542. write_lock(&map_tree->map_tree.lock);
  3543. ret = add_extent_mapping(&map_tree->map_tree, em);
  3544. write_unlock(&map_tree->map_tree.lock);
  3545. BUG_ON(ret);
  3546. free_extent_map(em);
  3547. return 0;
  3548. }
  3549. static int fill_device_from_item(struct extent_buffer *leaf,
  3550. struct btrfs_dev_item *dev_item,
  3551. struct btrfs_device *device)
  3552. {
  3553. unsigned long ptr;
  3554. device->devid = btrfs_device_id(leaf, dev_item);
  3555. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3556. device->total_bytes = device->disk_total_bytes;
  3557. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3558. device->type = btrfs_device_type(leaf, dev_item);
  3559. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3560. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3561. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3562. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3563. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3564. return 0;
  3565. }
  3566. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3567. {
  3568. struct btrfs_fs_devices *fs_devices;
  3569. int ret;
  3570. mutex_lock(&uuid_mutex);
  3571. fs_devices = root->fs_info->fs_devices->seed;
  3572. while (fs_devices) {
  3573. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3574. ret = 0;
  3575. goto out;
  3576. }
  3577. fs_devices = fs_devices->seed;
  3578. }
  3579. fs_devices = find_fsid(fsid);
  3580. if (!fs_devices) {
  3581. ret = -ENOENT;
  3582. goto out;
  3583. }
  3584. fs_devices = clone_fs_devices(fs_devices);
  3585. if (IS_ERR(fs_devices)) {
  3586. ret = PTR_ERR(fs_devices);
  3587. goto out;
  3588. }
  3589. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3590. root->fs_info->bdev_holder);
  3591. if (ret)
  3592. goto out;
  3593. if (!fs_devices->seeding) {
  3594. __btrfs_close_devices(fs_devices);
  3595. free_fs_devices(fs_devices);
  3596. ret = -EINVAL;
  3597. goto out;
  3598. }
  3599. fs_devices->seed = root->fs_info->fs_devices->seed;
  3600. root->fs_info->fs_devices->seed = fs_devices;
  3601. out:
  3602. mutex_unlock(&uuid_mutex);
  3603. return ret;
  3604. }
  3605. static int read_one_dev(struct btrfs_root *root,
  3606. struct extent_buffer *leaf,
  3607. struct btrfs_dev_item *dev_item)
  3608. {
  3609. struct btrfs_device *device;
  3610. u64 devid;
  3611. int ret;
  3612. u8 fs_uuid[BTRFS_UUID_SIZE];
  3613. u8 dev_uuid[BTRFS_UUID_SIZE];
  3614. devid = btrfs_device_id(leaf, dev_item);
  3615. read_extent_buffer(leaf, dev_uuid,
  3616. (unsigned long)btrfs_device_uuid(dev_item),
  3617. BTRFS_UUID_SIZE);
  3618. read_extent_buffer(leaf, fs_uuid,
  3619. (unsigned long)btrfs_device_fsid(dev_item),
  3620. BTRFS_UUID_SIZE);
  3621. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3622. ret = open_seed_devices(root, fs_uuid);
  3623. if (ret && !btrfs_test_opt(root, DEGRADED))
  3624. return ret;
  3625. }
  3626. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3627. if (!device || !device->bdev) {
  3628. if (!btrfs_test_opt(root, DEGRADED))
  3629. return -EIO;
  3630. if (!device) {
  3631. printk(KERN_WARNING "warning devid %llu missing\n",
  3632. (unsigned long long)devid);
  3633. device = add_missing_dev(root, devid, dev_uuid);
  3634. if (!device)
  3635. return -ENOMEM;
  3636. } else if (!device->missing) {
  3637. /*
  3638. * this happens when a device that was properly setup
  3639. * in the device info lists suddenly goes bad.
  3640. * device->bdev is NULL, and so we have to set
  3641. * device->missing to one here
  3642. */
  3643. root->fs_info->fs_devices->missing_devices++;
  3644. device->missing = 1;
  3645. }
  3646. }
  3647. if (device->fs_devices != root->fs_info->fs_devices) {
  3648. BUG_ON(device->writeable);
  3649. if (device->generation !=
  3650. btrfs_device_generation(leaf, dev_item))
  3651. return -EINVAL;
  3652. }
  3653. fill_device_from_item(leaf, dev_item, device);
  3654. device->dev_root = root->fs_info->dev_root;
  3655. device->in_fs_metadata = 1;
  3656. if (device->writeable) {
  3657. device->fs_devices->total_rw_bytes += device->total_bytes;
  3658. spin_lock(&root->fs_info->free_chunk_lock);
  3659. root->fs_info->free_chunk_space += device->total_bytes -
  3660. device->bytes_used;
  3661. spin_unlock(&root->fs_info->free_chunk_lock);
  3662. }
  3663. ret = 0;
  3664. return ret;
  3665. }
  3666. int btrfs_read_sys_array(struct btrfs_root *root)
  3667. {
  3668. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3669. struct extent_buffer *sb;
  3670. struct btrfs_disk_key *disk_key;
  3671. struct btrfs_chunk *chunk;
  3672. u8 *ptr;
  3673. unsigned long sb_ptr;
  3674. int ret = 0;
  3675. u32 num_stripes;
  3676. u32 array_size;
  3677. u32 len = 0;
  3678. u32 cur;
  3679. struct btrfs_key key;
  3680. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3681. BTRFS_SUPER_INFO_SIZE);
  3682. if (!sb)
  3683. return -ENOMEM;
  3684. btrfs_set_buffer_uptodate(sb);
  3685. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3686. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3687. array_size = btrfs_super_sys_array_size(super_copy);
  3688. ptr = super_copy->sys_chunk_array;
  3689. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3690. cur = 0;
  3691. while (cur < array_size) {
  3692. disk_key = (struct btrfs_disk_key *)ptr;
  3693. btrfs_disk_key_to_cpu(&key, disk_key);
  3694. len = sizeof(*disk_key); ptr += len;
  3695. sb_ptr += len;
  3696. cur += len;
  3697. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3698. chunk = (struct btrfs_chunk *)sb_ptr;
  3699. ret = read_one_chunk(root, &key, sb, chunk);
  3700. if (ret)
  3701. break;
  3702. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3703. len = btrfs_chunk_item_size(num_stripes);
  3704. } else {
  3705. ret = -EIO;
  3706. break;
  3707. }
  3708. ptr += len;
  3709. sb_ptr += len;
  3710. cur += len;
  3711. }
  3712. free_extent_buffer(sb);
  3713. return ret;
  3714. }
  3715. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3716. {
  3717. struct btrfs_path *path;
  3718. struct extent_buffer *leaf;
  3719. struct btrfs_key key;
  3720. struct btrfs_key found_key;
  3721. int ret;
  3722. int slot;
  3723. root = root->fs_info->chunk_root;
  3724. path = btrfs_alloc_path();
  3725. if (!path)
  3726. return -ENOMEM;
  3727. /* first we search for all of the device items, and then we
  3728. * read in all of the chunk items. This way we can create chunk
  3729. * mappings that reference all of the devices that are afound
  3730. */
  3731. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3732. key.offset = 0;
  3733. key.type = 0;
  3734. again:
  3735. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3736. if (ret < 0)
  3737. goto error;
  3738. while (1) {
  3739. leaf = path->nodes[0];
  3740. slot = path->slots[0];
  3741. if (slot >= btrfs_header_nritems(leaf)) {
  3742. ret = btrfs_next_leaf(root, path);
  3743. if (ret == 0)
  3744. continue;
  3745. if (ret < 0)
  3746. goto error;
  3747. break;
  3748. }
  3749. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3750. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3751. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3752. break;
  3753. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3754. struct btrfs_dev_item *dev_item;
  3755. dev_item = btrfs_item_ptr(leaf, slot,
  3756. struct btrfs_dev_item);
  3757. ret = read_one_dev(root, leaf, dev_item);
  3758. if (ret)
  3759. goto error;
  3760. }
  3761. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3762. struct btrfs_chunk *chunk;
  3763. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3764. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3765. if (ret)
  3766. goto error;
  3767. }
  3768. path->slots[0]++;
  3769. }
  3770. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3771. key.objectid = 0;
  3772. btrfs_release_path(path);
  3773. goto again;
  3774. }
  3775. ret = 0;
  3776. error:
  3777. btrfs_free_path(path);
  3778. return ret;
  3779. }