volumes.c 125 KB

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