volumes.c 124 KB

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