volumes.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039
  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 "compat.h"
  29. #include "ctree.h"
  30. #include "extent_map.h"
  31. #include "disk-io.h"
  32. #include "transaction.h"
  33. #include "print-tree.h"
  34. #include "volumes.h"
  35. #include "async-thread.h"
  36. #include "check-integrity.h"
  37. #include "rcu-string.h"
  38. #include "math.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 "btrfs: 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. root->fs_info->num_tolerated_disk_barrier_failures =
  1278. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1279. /*
  1280. * at this point, the device is zero sized. We want to
  1281. * remove it from the devices list and zero out the old super
  1282. */
  1283. if (clear_super) {
  1284. /* make sure this device isn't detected as part of
  1285. * the FS anymore
  1286. */
  1287. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1288. set_buffer_dirty(bh);
  1289. sync_dirty_buffer(bh);
  1290. }
  1291. ret = 0;
  1292. error_brelse:
  1293. brelse(bh);
  1294. error_close:
  1295. if (bdev)
  1296. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1297. out:
  1298. mutex_unlock(&uuid_mutex);
  1299. return ret;
  1300. error_undo:
  1301. if (device->writeable) {
  1302. lock_chunks(root);
  1303. list_add(&device->dev_alloc_list,
  1304. &root->fs_info->fs_devices->alloc_list);
  1305. unlock_chunks(root);
  1306. root->fs_info->fs_devices->rw_devices++;
  1307. }
  1308. goto error_brelse;
  1309. }
  1310. /*
  1311. * does all the dirty work required for changing file system's UUID.
  1312. */
  1313. static int btrfs_prepare_sprout(struct btrfs_root *root)
  1314. {
  1315. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1316. struct btrfs_fs_devices *old_devices;
  1317. struct btrfs_fs_devices *seed_devices;
  1318. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1319. struct btrfs_device *device;
  1320. u64 super_flags;
  1321. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1322. if (!fs_devices->seeding)
  1323. return -EINVAL;
  1324. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1325. if (!seed_devices)
  1326. return -ENOMEM;
  1327. old_devices = clone_fs_devices(fs_devices);
  1328. if (IS_ERR(old_devices)) {
  1329. kfree(seed_devices);
  1330. return PTR_ERR(old_devices);
  1331. }
  1332. list_add(&old_devices->list, &fs_uuids);
  1333. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1334. seed_devices->opened = 1;
  1335. INIT_LIST_HEAD(&seed_devices->devices);
  1336. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1337. mutex_init(&seed_devices->device_list_mutex);
  1338. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1339. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1340. synchronize_rcu);
  1341. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1342. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1343. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1344. device->fs_devices = seed_devices;
  1345. }
  1346. fs_devices->seeding = 0;
  1347. fs_devices->num_devices = 0;
  1348. fs_devices->open_devices = 0;
  1349. fs_devices->total_devices = 0;
  1350. fs_devices->seed = seed_devices;
  1351. generate_random_uuid(fs_devices->fsid);
  1352. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1353. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1354. super_flags = btrfs_super_flags(disk_super) &
  1355. ~BTRFS_SUPER_FLAG_SEEDING;
  1356. btrfs_set_super_flags(disk_super, super_flags);
  1357. return 0;
  1358. }
  1359. /*
  1360. * strore the expected generation for seed devices in device items.
  1361. */
  1362. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1363. struct btrfs_root *root)
  1364. {
  1365. struct btrfs_path *path;
  1366. struct extent_buffer *leaf;
  1367. struct btrfs_dev_item *dev_item;
  1368. struct btrfs_device *device;
  1369. struct btrfs_key key;
  1370. u8 fs_uuid[BTRFS_UUID_SIZE];
  1371. u8 dev_uuid[BTRFS_UUID_SIZE];
  1372. u64 devid;
  1373. int ret;
  1374. path = btrfs_alloc_path();
  1375. if (!path)
  1376. return -ENOMEM;
  1377. root = root->fs_info->chunk_root;
  1378. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1379. key.offset = 0;
  1380. key.type = BTRFS_DEV_ITEM_KEY;
  1381. while (1) {
  1382. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1383. if (ret < 0)
  1384. goto error;
  1385. leaf = path->nodes[0];
  1386. next_slot:
  1387. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1388. ret = btrfs_next_leaf(root, path);
  1389. if (ret > 0)
  1390. break;
  1391. if (ret < 0)
  1392. goto error;
  1393. leaf = path->nodes[0];
  1394. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1395. btrfs_release_path(path);
  1396. continue;
  1397. }
  1398. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1399. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1400. key.type != BTRFS_DEV_ITEM_KEY)
  1401. break;
  1402. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1403. struct btrfs_dev_item);
  1404. devid = btrfs_device_id(leaf, dev_item);
  1405. read_extent_buffer(leaf, dev_uuid,
  1406. (unsigned long)btrfs_device_uuid(dev_item),
  1407. BTRFS_UUID_SIZE);
  1408. read_extent_buffer(leaf, fs_uuid,
  1409. (unsigned long)btrfs_device_fsid(dev_item),
  1410. BTRFS_UUID_SIZE);
  1411. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1412. BUG_ON(!device); /* Logic error */
  1413. if (device->fs_devices->seeding) {
  1414. btrfs_set_device_generation(leaf, dev_item,
  1415. device->generation);
  1416. btrfs_mark_buffer_dirty(leaf);
  1417. }
  1418. path->slots[0]++;
  1419. goto next_slot;
  1420. }
  1421. ret = 0;
  1422. error:
  1423. btrfs_free_path(path);
  1424. return ret;
  1425. }
  1426. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1427. {
  1428. struct request_queue *q;
  1429. struct btrfs_trans_handle *trans;
  1430. struct btrfs_device *device;
  1431. struct block_device *bdev;
  1432. struct list_head *devices;
  1433. struct super_block *sb = root->fs_info->sb;
  1434. struct rcu_string *name;
  1435. u64 total_bytes;
  1436. int seeding_dev = 0;
  1437. int ret = 0;
  1438. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1439. return -EROFS;
  1440. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1441. root->fs_info->bdev_holder);
  1442. if (IS_ERR(bdev))
  1443. return PTR_ERR(bdev);
  1444. if (root->fs_info->fs_devices->seeding) {
  1445. seeding_dev = 1;
  1446. down_write(&sb->s_umount);
  1447. mutex_lock(&uuid_mutex);
  1448. }
  1449. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1450. devices = &root->fs_info->fs_devices->devices;
  1451. /*
  1452. * we have the volume lock, so we don't need the extra
  1453. * device list mutex while reading the list here.
  1454. */
  1455. list_for_each_entry(device, devices, dev_list) {
  1456. if (device->bdev == bdev) {
  1457. ret = -EEXIST;
  1458. goto error;
  1459. }
  1460. }
  1461. device = kzalloc(sizeof(*device), GFP_NOFS);
  1462. if (!device) {
  1463. /* we can safely leave the fs_devices entry around */
  1464. ret = -ENOMEM;
  1465. goto error;
  1466. }
  1467. name = rcu_string_strdup(device_path, GFP_NOFS);
  1468. if (!name) {
  1469. kfree(device);
  1470. ret = -ENOMEM;
  1471. goto error;
  1472. }
  1473. rcu_assign_pointer(device->name, name);
  1474. ret = find_next_devid(root, &device->devid);
  1475. if (ret) {
  1476. rcu_string_free(device->name);
  1477. kfree(device);
  1478. goto error;
  1479. }
  1480. trans = btrfs_start_transaction(root, 0);
  1481. if (IS_ERR(trans)) {
  1482. rcu_string_free(device->name);
  1483. kfree(device);
  1484. ret = PTR_ERR(trans);
  1485. goto error;
  1486. }
  1487. lock_chunks(root);
  1488. q = bdev_get_queue(bdev);
  1489. if (blk_queue_discard(q))
  1490. device->can_discard = 1;
  1491. device->writeable = 1;
  1492. device->work.func = pending_bios_fn;
  1493. generate_random_uuid(device->uuid);
  1494. spin_lock_init(&device->io_lock);
  1495. device->generation = trans->transid;
  1496. device->io_width = root->sectorsize;
  1497. device->io_align = root->sectorsize;
  1498. device->sector_size = root->sectorsize;
  1499. device->total_bytes = i_size_read(bdev->bd_inode);
  1500. device->disk_total_bytes = device->total_bytes;
  1501. device->dev_root = root->fs_info->dev_root;
  1502. device->bdev = bdev;
  1503. device->in_fs_metadata = 1;
  1504. device->mode = FMODE_EXCL;
  1505. set_blocksize(device->bdev, 4096);
  1506. if (seeding_dev) {
  1507. sb->s_flags &= ~MS_RDONLY;
  1508. ret = btrfs_prepare_sprout(root);
  1509. BUG_ON(ret); /* -ENOMEM */
  1510. }
  1511. device->fs_devices = root->fs_info->fs_devices;
  1512. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1513. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1514. list_add(&device->dev_alloc_list,
  1515. &root->fs_info->fs_devices->alloc_list);
  1516. root->fs_info->fs_devices->num_devices++;
  1517. root->fs_info->fs_devices->open_devices++;
  1518. root->fs_info->fs_devices->rw_devices++;
  1519. root->fs_info->fs_devices->total_devices++;
  1520. if (device->can_discard)
  1521. root->fs_info->fs_devices->num_can_discard++;
  1522. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1523. spin_lock(&root->fs_info->free_chunk_lock);
  1524. root->fs_info->free_chunk_space += device->total_bytes;
  1525. spin_unlock(&root->fs_info->free_chunk_lock);
  1526. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1527. root->fs_info->fs_devices->rotating = 1;
  1528. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1529. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1530. total_bytes + device->total_bytes);
  1531. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1532. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1533. total_bytes + 1);
  1534. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1535. if (seeding_dev) {
  1536. ret = init_first_rw_device(trans, root, device);
  1537. if (ret) {
  1538. btrfs_abort_transaction(trans, root, ret);
  1539. goto error_trans;
  1540. }
  1541. ret = btrfs_finish_sprout(trans, root);
  1542. if (ret) {
  1543. btrfs_abort_transaction(trans, root, ret);
  1544. goto error_trans;
  1545. }
  1546. } else {
  1547. ret = btrfs_add_device(trans, root, device);
  1548. if (ret) {
  1549. btrfs_abort_transaction(trans, root, ret);
  1550. goto error_trans;
  1551. }
  1552. }
  1553. /*
  1554. * we've got more storage, clear any full flags on the space
  1555. * infos
  1556. */
  1557. btrfs_clear_space_info_full(root->fs_info);
  1558. unlock_chunks(root);
  1559. root->fs_info->num_tolerated_disk_barrier_failures =
  1560. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1561. ret = btrfs_commit_transaction(trans, root);
  1562. if (seeding_dev) {
  1563. mutex_unlock(&uuid_mutex);
  1564. up_write(&sb->s_umount);
  1565. if (ret) /* transaction commit */
  1566. return ret;
  1567. ret = btrfs_relocate_sys_chunks(root);
  1568. if (ret < 0)
  1569. btrfs_error(root->fs_info, ret,
  1570. "Failed to relocate sys chunks after "
  1571. "device initialization. This can be fixed "
  1572. "using the \"btrfs balance\" command.");
  1573. trans = btrfs_attach_transaction(root);
  1574. if (IS_ERR(trans)) {
  1575. if (PTR_ERR(trans) == -ENOENT)
  1576. return 0;
  1577. return PTR_ERR(trans);
  1578. }
  1579. ret = btrfs_commit_transaction(trans, root);
  1580. }
  1581. return ret;
  1582. error_trans:
  1583. unlock_chunks(root);
  1584. btrfs_end_transaction(trans, root);
  1585. rcu_string_free(device->name);
  1586. kfree(device);
  1587. error:
  1588. blkdev_put(bdev, FMODE_EXCL);
  1589. if (seeding_dev) {
  1590. mutex_unlock(&uuid_mutex);
  1591. up_write(&sb->s_umount);
  1592. }
  1593. return ret;
  1594. }
  1595. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1596. struct btrfs_device *device)
  1597. {
  1598. int ret;
  1599. struct btrfs_path *path;
  1600. struct btrfs_root *root;
  1601. struct btrfs_dev_item *dev_item;
  1602. struct extent_buffer *leaf;
  1603. struct btrfs_key key;
  1604. root = device->dev_root->fs_info->chunk_root;
  1605. path = btrfs_alloc_path();
  1606. if (!path)
  1607. return -ENOMEM;
  1608. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1609. key.type = BTRFS_DEV_ITEM_KEY;
  1610. key.offset = device->devid;
  1611. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1612. if (ret < 0)
  1613. goto out;
  1614. if (ret > 0) {
  1615. ret = -ENOENT;
  1616. goto out;
  1617. }
  1618. leaf = path->nodes[0];
  1619. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1620. btrfs_set_device_id(leaf, dev_item, device->devid);
  1621. btrfs_set_device_type(leaf, dev_item, device->type);
  1622. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1623. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1624. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1625. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1626. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1627. btrfs_mark_buffer_dirty(leaf);
  1628. out:
  1629. btrfs_free_path(path);
  1630. return ret;
  1631. }
  1632. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1633. struct btrfs_device *device, u64 new_size)
  1634. {
  1635. struct btrfs_super_block *super_copy =
  1636. device->dev_root->fs_info->super_copy;
  1637. u64 old_total = btrfs_super_total_bytes(super_copy);
  1638. u64 diff = new_size - device->total_bytes;
  1639. if (!device->writeable)
  1640. return -EACCES;
  1641. if (new_size <= device->total_bytes)
  1642. return -EINVAL;
  1643. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1644. device->fs_devices->total_rw_bytes += diff;
  1645. device->total_bytes = new_size;
  1646. device->disk_total_bytes = new_size;
  1647. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1648. return btrfs_update_device(trans, device);
  1649. }
  1650. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1651. struct btrfs_device *device, u64 new_size)
  1652. {
  1653. int ret;
  1654. lock_chunks(device->dev_root);
  1655. ret = __btrfs_grow_device(trans, device, new_size);
  1656. unlock_chunks(device->dev_root);
  1657. return ret;
  1658. }
  1659. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1660. struct btrfs_root *root,
  1661. u64 chunk_tree, u64 chunk_objectid,
  1662. u64 chunk_offset)
  1663. {
  1664. int ret;
  1665. struct btrfs_path *path;
  1666. struct btrfs_key key;
  1667. root = root->fs_info->chunk_root;
  1668. path = btrfs_alloc_path();
  1669. if (!path)
  1670. return -ENOMEM;
  1671. key.objectid = chunk_objectid;
  1672. key.offset = chunk_offset;
  1673. key.type = BTRFS_CHUNK_ITEM_KEY;
  1674. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1675. if (ret < 0)
  1676. goto out;
  1677. else if (ret > 0) { /* Logic error or corruption */
  1678. btrfs_error(root->fs_info, -ENOENT,
  1679. "Failed lookup while freeing chunk.");
  1680. ret = -ENOENT;
  1681. goto out;
  1682. }
  1683. ret = btrfs_del_item(trans, root, path);
  1684. if (ret < 0)
  1685. btrfs_error(root->fs_info, ret,
  1686. "Failed to delete chunk item.");
  1687. out:
  1688. btrfs_free_path(path);
  1689. return ret;
  1690. }
  1691. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1692. chunk_offset)
  1693. {
  1694. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1695. struct btrfs_disk_key *disk_key;
  1696. struct btrfs_chunk *chunk;
  1697. u8 *ptr;
  1698. int ret = 0;
  1699. u32 num_stripes;
  1700. u32 array_size;
  1701. u32 len = 0;
  1702. u32 cur;
  1703. struct btrfs_key key;
  1704. array_size = btrfs_super_sys_array_size(super_copy);
  1705. ptr = super_copy->sys_chunk_array;
  1706. cur = 0;
  1707. while (cur < array_size) {
  1708. disk_key = (struct btrfs_disk_key *)ptr;
  1709. btrfs_disk_key_to_cpu(&key, disk_key);
  1710. len = sizeof(*disk_key);
  1711. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1712. chunk = (struct btrfs_chunk *)(ptr + len);
  1713. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1714. len += btrfs_chunk_item_size(num_stripes);
  1715. } else {
  1716. ret = -EIO;
  1717. break;
  1718. }
  1719. if (key.objectid == chunk_objectid &&
  1720. key.offset == chunk_offset) {
  1721. memmove(ptr, ptr + len, array_size - (cur + len));
  1722. array_size -= len;
  1723. btrfs_set_super_sys_array_size(super_copy, array_size);
  1724. } else {
  1725. ptr += len;
  1726. cur += len;
  1727. }
  1728. }
  1729. return ret;
  1730. }
  1731. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1732. u64 chunk_tree, u64 chunk_objectid,
  1733. u64 chunk_offset)
  1734. {
  1735. struct extent_map_tree *em_tree;
  1736. struct btrfs_root *extent_root;
  1737. struct btrfs_trans_handle *trans;
  1738. struct extent_map *em;
  1739. struct map_lookup *map;
  1740. int ret;
  1741. int i;
  1742. root = root->fs_info->chunk_root;
  1743. extent_root = root->fs_info->extent_root;
  1744. em_tree = &root->fs_info->mapping_tree.map_tree;
  1745. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1746. if (ret)
  1747. return -ENOSPC;
  1748. /* step one, relocate all the extents inside this chunk */
  1749. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1750. if (ret)
  1751. return ret;
  1752. trans = btrfs_start_transaction(root, 0);
  1753. BUG_ON(IS_ERR(trans));
  1754. lock_chunks(root);
  1755. /*
  1756. * step two, delete the device extents and the
  1757. * chunk tree entries
  1758. */
  1759. read_lock(&em_tree->lock);
  1760. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1761. read_unlock(&em_tree->lock);
  1762. BUG_ON(!em || em->start > chunk_offset ||
  1763. em->start + em->len < chunk_offset);
  1764. map = (struct map_lookup *)em->bdev;
  1765. for (i = 0; i < map->num_stripes; i++) {
  1766. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1767. map->stripes[i].physical);
  1768. BUG_ON(ret);
  1769. if (map->stripes[i].dev) {
  1770. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1771. BUG_ON(ret);
  1772. }
  1773. }
  1774. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1775. chunk_offset);
  1776. BUG_ON(ret);
  1777. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1778. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1779. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1780. BUG_ON(ret);
  1781. }
  1782. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1783. BUG_ON(ret);
  1784. write_lock(&em_tree->lock);
  1785. remove_extent_mapping(em_tree, em);
  1786. write_unlock(&em_tree->lock);
  1787. kfree(map);
  1788. em->bdev = NULL;
  1789. /* once for the tree */
  1790. free_extent_map(em);
  1791. /* once for us */
  1792. free_extent_map(em);
  1793. unlock_chunks(root);
  1794. btrfs_end_transaction(trans, root);
  1795. return 0;
  1796. }
  1797. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1798. {
  1799. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1800. struct btrfs_path *path;
  1801. struct extent_buffer *leaf;
  1802. struct btrfs_chunk *chunk;
  1803. struct btrfs_key key;
  1804. struct btrfs_key found_key;
  1805. u64 chunk_tree = chunk_root->root_key.objectid;
  1806. u64 chunk_type;
  1807. bool retried = false;
  1808. int failed = 0;
  1809. int ret;
  1810. path = btrfs_alloc_path();
  1811. if (!path)
  1812. return -ENOMEM;
  1813. again:
  1814. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1815. key.offset = (u64)-1;
  1816. key.type = BTRFS_CHUNK_ITEM_KEY;
  1817. while (1) {
  1818. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1819. if (ret < 0)
  1820. goto error;
  1821. BUG_ON(ret == 0); /* Corruption */
  1822. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1823. key.type);
  1824. if (ret < 0)
  1825. goto error;
  1826. if (ret > 0)
  1827. break;
  1828. leaf = path->nodes[0];
  1829. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1830. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1831. struct btrfs_chunk);
  1832. chunk_type = btrfs_chunk_type(leaf, chunk);
  1833. btrfs_release_path(path);
  1834. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1835. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1836. found_key.objectid,
  1837. found_key.offset);
  1838. if (ret == -ENOSPC)
  1839. failed++;
  1840. else if (ret)
  1841. BUG();
  1842. }
  1843. if (found_key.offset == 0)
  1844. break;
  1845. key.offset = found_key.offset - 1;
  1846. }
  1847. ret = 0;
  1848. if (failed && !retried) {
  1849. failed = 0;
  1850. retried = true;
  1851. goto again;
  1852. } else if (failed && retried) {
  1853. WARN_ON(1);
  1854. ret = -ENOSPC;
  1855. }
  1856. error:
  1857. btrfs_free_path(path);
  1858. return ret;
  1859. }
  1860. static int insert_balance_item(struct btrfs_root *root,
  1861. struct btrfs_balance_control *bctl)
  1862. {
  1863. struct btrfs_trans_handle *trans;
  1864. struct btrfs_balance_item *item;
  1865. struct btrfs_disk_balance_args disk_bargs;
  1866. struct btrfs_path *path;
  1867. struct extent_buffer *leaf;
  1868. struct btrfs_key key;
  1869. int ret, err;
  1870. path = btrfs_alloc_path();
  1871. if (!path)
  1872. return -ENOMEM;
  1873. trans = btrfs_start_transaction(root, 0);
  1874. if (IS_ERR(trans)) {
  1875. btrfs_free_path(path);
  1876. return PTR_ERR(trans);
  1877. }
  1878. key.objectid = BTRFS_BALANCE_OBJECTID;
  1879. key.type = BTRFS_BALANCE_ITEM_KEY;
  1880. key.offset = 0;
  1881. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1882. sizeof(*item));
  1883. if (ret)
  1884. goto out;
  1885. leaf = path->nodes[0];
  1886. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  1887. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  1888. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  1889. btrfs_set_balance_data(leaf, item, &disk_bargs);
  1890. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  1891. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  1892. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  1893. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  1894. btrfs_set_balance_flags(leaf, item, bctl->flags);
  1895. btrfs_mark_buffer_dirty(leaf);
  1896. out:
  1897. btrfs_free_path(path);
  1898. err = btrfs_commit_transaction(trans, root);
  1899. if (err && !ret)
  1900. ret = err;
  1901. return ret;
  1902. }
  1903. static int del_balance_item(struct btrfs_root *root)
  1904. {
  1905. struct btrfs_trans_handle *trans;
  1906. struct btrfs_path *path;
  1907. struct btrfs_key key;
  1908. int ret, err;
  1909. path = btrfs_alloc_path();
  1910. if (!path)
  1911. return -ENOMEM;
  1912. trans = btrfs_start_transaction(root, 0);
  1913. if (IS_ERR(trans)) {
  1914. btrfs_free_path(path);
  1915. return PTR_ERR(trans);
  1916. }
  1917. key.objectid = BTRFS_BALANCE_OBJECTID;
  1918. key.type = BTRFS_BALANCE_ITEM_KEY;
  1919. key.offset = 0;
  1920. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1921. if (ret < 0)
  1922. goto out;
  1923. if (ret > 0) {
  1924. ret = -ENOENT;
  1925. goto out;
  1926. }
  1927. ret = btrfs_del_item(trans, root, path);
  1928. out:
  1929. btrfs_free_path(path);
  1930. err = btrfs_commit_transaction(trans, root);
  1931. if (err && !ret)
  1932. ret = err;
  1933. return ret;
  1934. }
  1935. /*
  1936. * This is a heuristic used to reduce the number of chunks balanced on
  1937. * resume after balance was interrupted.
  1938. */
  1939. static void update_balance_args(struct btrfs_balance_control *bctl)
  1940. {
  1941. /*
  1942. * Turn on soft mode for chunk types that were being converted.
  1943. */
  1944. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  1945. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  1946. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  1947. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  1948. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  1949. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  1950. /*
  1951. * Turn on usage filter if is not already used. The idea is
  1952. * that chunks that we have already balanced should be
  1953. * reasonably full. Don't do it for chunks that are being
  1954. * converted - that will keep us from relocating unconverted
  1955. * (albeit full) chunks.
  1956. */
  1957. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  1958. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  1959. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  1960. bctl->data.usage = 90;
  1961. }
  1962. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  1963. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  1964. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  1965. bctl->sys.usage = 90;
  1966. }
  1967. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  1968. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  1969. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  1970. bctl->meta.usage = 90;
  1971. }
  1972. }
  1973. /*
  1974. * Should be called with both balance and volume mutexes held to
  1975. * serialize other volume operations (add_dev/rm_dev/resize) with
  1976. * restriper. Same goes for unset_balance_control.
  1977. */
  1978. static void set_balance_control(struct btrfs_balance_control *bctl)
  1979. {
  1980. struct btrfs_fs_info *fs_info = bctl->fs_info;
  1981. BUG_ON(fs_info->balance_ctl);
  1982. spin_lock(&fs_info->balance_lock);
  1983. fs_info->balance_ctl = bctl;
  1984. spin_unlock(&fs_info->balance_lock);
  1985. }
  1986. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  1987. {
  1988. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  1989. BUG_ON(!fs_info->balance_ctl);
  1990. spin_lock(&fs_info->balance_lock);
  1991. fs_info->balance_ctl = NULL;
  1992. spin_unlock(&fs_info->balance_lock);
  1993. kfree(bctl);
  1994. }
  1995. /*
  1996. * Balance filters. Return 1 if chunk should be filtered out
  1997. * (should not be balanced).
  1998. */
  1999. static int chunk_profiles_filter(u64 chunk_type,
  2000. struct btrfs_balance_args *bargs)
  2001. {
  2002. chunk_type = chunk_to_extended(chunk_type) &
  2003. BTRFS_EXTENDED_PROFILE_MASK;
  2004. if (bargs->profiles & chunk_type)
  2005. return 0;
  2006. return 1;
  2007. }
  2008. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2009. struct btrfs_balance_args *bargs)
  2010. {
  2011. struct btrfs_block_group_cache *cache;
  2012. u64 chunk_used, user_thresh;
  2013. int ret = 1;
  2014. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2015. chunk_used = btrfs_block_group_used(&cache->item);
  2016. user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
  2017. if (chunk_used < user_thresh)
  2018. ret = 0;
  2019. btrfs_put_block_group(cache);
  2020. return ret;
  2021. }
  2022. static int chunk_devid_filter(struct extent_buffer *leaf,
  2023. struct btrfs_chunk *chunk,
  2024. struct btrfs_balance_args *bargs)
  2025. {
  2026. struct btrfs_stripe *stripe;
  2027. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2028. int i;
  2029. for (i = 0; i < num_stripes; i++) {
  2030. stripe = btrfs_stripe_nr(chunk, i);
  2031. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2032. return 0;
  2033. }
  2034. return 1;
  2035. }
  2036. /* [pstart, pend) */
  2037. static int chunk_drange_filter(struct extent_buffer *leaf,
  2038. struct btrfs_chunk *chunk,
  2039. u64 chunk_offset,
  2040. struct btrfs_balance_args *bargs)
  2041. {
  2042. struct btrfs_stripe *stripe;
  2043. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2044. u64 stripe_offset;
  2045. u64 stripe_length;
  2046. int factor;
  2047. int i;
  2048. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2049. return 0;
  2050. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2051. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
  2052. factor = 2;
  2053. else
  2054. factor = 1;
  2055. factor = num_stripes / factor;
  2056. for (i = 0; i < num_stripes; i++) {
  2057. stripe = btrfs_stripe_nr(chunk, i);
  2058. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2059. continue;
  2060. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2061. stripe_length = btrfs_chunk_length(leaf, chunk);
  2062. do_div(stripe_length, factor);
  2063. if (stripe_offset < bargs->pend &&
  2064. stripe_offset + stripe_length > bargs->pstart)
  2065. return 0;
  2066. }
  2067. return 1;
  2068. }
  2069. /* [vstart, vend) */
  2070. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2071. struct btrfs_chunk *chunk,
  2072. u64 chunk_offset,
  2073. struct btrfs_balance_args *bargs)
  2074. {
  2075. if (chunk_offset < bargs->vend &&
  2076. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2077. /* at least part of the chunk is inside this vrange */
  2078. return 0;
  2079. return 1;
  2080. }
  2081. static int chunk_soft_convert_filter(u64 chunk_type,
  2082. struct btrfs_balance_args *bargs)
  2083. {
  2084. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2085. return 0;
  2086. chunk_type = chunk_to_extended(chunk_type) &
  2087. BTRFS_EXTENDED_PROFILE_MASK;
  2088. if (bargs->target == chunk_type)
  2089. return 1;
  2090. return 0;
  2091. }
  2092. static int should_balance_chunk(struct btrfs_root *root,
  2093. struct extent_buffer *leaf,
  2094. struct btrfs_chunk *chunk, u64 chunk_offset)
  2095. {
  2096. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2097. struct btrfs_balance_args *bargs = NULL;
  2098. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2099. /* type filter */
  2100. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2101. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2102. return 0;
  2103. }
  2104. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2105. bargs = &bctl->data;
  2106. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2107. bargs = &bctl->sys;
  2108. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2109. bargs = &bctl->meta;
  2110. /* profiles filter */
  2111. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2112. chunk_profiles_filter(chunk_type, bargs)) {
  2113. return 0;
  2114. }
  2115. /* usage filter */
  2116. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2117. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  2118. return 0;
  2119. }
  2120. /* devid filter */
  2121. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2122. chunk_devid_filter(leaf, chunk, bargs)) {
  2123. return 0;
  2124. }
  2125. /* drange filter, makes sense only with devid filter */
  2126. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2127. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  2128. return 0;
  2129. }
  2130. /* vrange filter */
  2131. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2132. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2133. return 0;
  2134. }
  2135. /* soft profile changing mode */
  2136. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2137. chunk_soft_convert_filter(chunk_type, bargs)) {
  2138. return 0;
  2139. }
  2140. return 1;
  2141. }
  2142. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2143. {
  2144. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2145. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2146. struct btrfs_root *dev_root = fs_info->dev_root;
  2147. struct list_head *devices;
  2148. struct btrfs_device *device;
  2149. u64 old_size;
  2150. u64 size_to_free;
  2151. struct btrfs_chunk *chunk;
  2152. struct btrfs_path *path;
  2153. struct btrfs_key key;
  2154. struct btrfs_key found_key;
  2155. struct btrfs_trans_handle *trans;
  2156. struct extent_buffer *leaf;
  2157. int slot;
  2158. int ret;
  2159. int enospc_errors = 0;
  2160. bool counting = true;
  2161. /* step one make some room on all the devices */
  2162. devices = &fs_info->fs_devices->devices;
  2163. list_for_each_entry(device, devices, dev_list) {
  2164. old_size = device->total_bytes;
  2165. size_to_free = div_factor(old_size, 1);
  2166. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2167. if (!device->writeable ||
  2168. device->total_bytes - device->bytes_used > size_to_free)
  2169. continue;
  2170. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2171. if (ret == -ENOSPC)
  2172. break;
  2173. BUG_ON(ret);
  2174. trans = btrfs_start_transaction(dev_root, 0);
  2175. BUG_ON(IS_ERR(trans));
  2176. ret = btrfs_grow_device(trans, device, old_size);
  2177. BUG_ON(ret);
  2178. btrfs_end_transaction(trans, dev_root);
  2179. }
  2180. /* step two, relocate all the chunks */
  2181. path = btrfs_alloc_path();
  2182. if (!path) {
  2183. ret = -ENOMEM;
  2184. goto error;
  2185. }
  2186. /* zero out stat counters */
  2187. spin_lock(&fs_info->balance_lock);
  2188. memset(&bctl->stat, 0, sizeof(bctl->stat));
  2189. spin_unlock(&fs_info->balance_lock);
  2190. again:
  2191. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2192. key.offset = (u64)-1;
  2193. key.type = BTRFS_CHUNK_ITEM_KEY;
  2194. while (1) {
  2195. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  2196. atomic_read(&fs_info->balance_cancel_req)) {
  2197. ret = -ECANCELED;
  2198. goto error;
  2199. }
  2200. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2201. if (ret < 0)
  2202. goto error;
  2203. /*
  2204. * this shouldn't happen, it means the last relocate
  2205. * failed
  2206. */
  2207. if (ret == 0)
  2208. BUG(); /* FIXME break ? */
  2209. ret = btrfs_previous_item(chunk_root, path, 0,
  2210. BTRFS_CHUNK_ITEM_KEY);
  2211. if (ret) {
  2212. ret = 0;
  2213. break;
  2214. }
  2215. leaf = path->nodes[0];
  2216. slot = path->slots[0];
  2217. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2218. if (found_key.objectid != key.objectid)
  2219. break;
  2220. /* chunk zero is special */
  2221. if (found_key.offset == 0)
  2222. break;
  2223. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2224. if (!counting) {
  2225. spin_lock(&fs_info->balance_lock);
  2226. bctl->stat.considered++;
  2227. spin_unlock(&fs_info->balance_lock);
  2228. }
  2229. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2230. found_key.offset);
  2231. btrfs_release_path(path);
  2232. if (!ret)
  2233. goto loop;
  2234. if (counting) {
  2235. spin_lock(&fs_info->balance_lock);
  2236. bctl->stat.expected++;
  2237. spin_unlock(&fs_info->balance_lock);
  2238. goto loop;
  2239. }
  2240. ret = btrfs_relocate_chunk(chunk_root,
  2241. chunk_root->root_key.objectid,
  2242. found_key.objectid,
  2243. found_key.offset);
  2244. if (ret && ret != -ENOSPC)
  2245. goto error;
  2246. if (ret == -ENOSPC) {
  2247. enospc_errors++;
  2248. } else {
  2249. spin_lock(&fs_info->balance_lock);
  2250. bctl->stat.completed++;
  2251. spin_unlock(&fs_info->balance_lock);
  2252. }
  2253. loop:
  2254. key.offset = found_key.offset - 1;
  2255. }
  2256. if (counting) {
  2257. btrfs_release_path(path);
  2258. counting = false;
  2259. goto again;
  2260. }
  2261. error:
  2262. btrfs_free_path(path);
  2263. if (enospc_errors) {
  2264. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2265. enospc_errors);
  2266. if (!ret)
  2267. ret = -ENOSPC;
  2268. }
  2269. return ret;
  2270. }
  2271. /**
  2272. * alloc_profile_is_valid - see if a given profile is valid and reduced
  2273. * @flags: profile to validate
  2274. * @extended: if true @flags is treated as an extended profile
  2275. */
  2276. static int alloc_profile_is_valid(u64 flags, int extended)
  2277. {
  2278. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  2279. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  2280. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  2281. /* 1) check that all other bits are zeroed */
  2282. if (flags & ~mask)
  2283. return 0;
  2284. /* 2) see if profile is reduced */
  2285. if (flags == 0)
  2286. return !extended; /* "0" is valid for usual profiles */
  2287. /* true if exactly one bit set */
  2288. return (flags & (flags - 1)) == 0;
  2289. }
  2290. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  2291. {
  2292. /* cancel requested || normal exit path */
  2293. return atomic_read(&fs_info->balance_cancel_req) ||
  2294. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  2295. atomic_read(&fs_info->balance_cancel_req) == 0);
  2296. }
  2297. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2298. {
  2299. int ret;
  2300. unset_balance_control(fs_info);
  2301. ret = del_balance_item(fs_info->tree_root);
  2302. BUG_ON(ret);
  2303. }
  2304. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2305. struct btrfs_ioctl_balance_args *bargs);
  2306. /*
  2307. * Should be called with both balance and volume mutexes held
  2308. */
  2309. int btrfs_balance(struct btrfs_balance_control *bctl,
  2310. struct btrfs_ioctl_balance_args *bargs)
  2311. {
  2312. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2313. u64 allowed;
  2314. int mixed = 0;
  2315. int ret;
  2316. if (btrfs_fs_closing(fs_info) ||
  2317. atomic_read(&fs_info->balance_pause_req) ||
  2318. atomic_read(&fs_info->balance_cancel_req)) {
  2319. ret = -EINVAL;
  2320. goto out;
  2321. }
  2322. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2323. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  2324. mixed = 1;
  2325. /*
  2326. * In case of mixed groups both data and meta should be picked,
  2327. * and identical options should be given for both of them.
  2328. */
  2329. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  2330. if (mixed && (bctl->flags & allowed)) {
  2331. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2332. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2333. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2334. printk(KERN_ERR "btrfs: with mixed groups data and "
  2335. "metadata balance options must be the same\n");
  2336. ret = -EINVAL;
  2337. goto out;
  2338. }
  2339. }
  2340. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2341. if (fs_info->fs_devices->num_devices == 1)
  2342. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2343. else if (fs_info->fs_devices->num_devices < 4)
  2344. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2345. else
  2346. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2347. BTRFS_BLOCK_GROUP_RAID10);
  2348. if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2349. (!alloc_profile_is_valid(bctl->data.target, 1) ||
  2350. (bctl->data.target & ~allowed))) {
  2351. printk(KERN_ERR "btrfs: unable to start balance with target "
  2352. "data profile %llu\n",
  2353. (unsigned long long)bctl->data.target);
  2354. ret = -EINVAL;
  2355. goto out;
  2356. }
  2357. if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2358. (!alloc_profile_is_valid(bctl->meta.target, 1) ||
  2359. (bctl->meta.target & ~allowed))) {
  2360. printk(KERN_ERR "btrfs: unable to start balance with target "
  2361. "metadata profile %llu\n",
  2362. (unsigned long long)bctl->meta.target);
  2363. ret = -EINVAL;
  2364. goto out;
  2365. }
  2366. if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2367. (!alloc_profile_is_valid(bctl->sys.target, 1) ||
  2368. (bctl->sys.target & ~allowed))) {
  2369. printk(KERN_ERR "btrfs: unable to start balance with target "
  2370. "system profile %llu\n",
  2371. (unsigned long long)bctl->sys.target);
  2372. ret = -EINVAL;
  2373. goto out;
  2374. }
  2375. /* allow dup'ed data chunks only in mixed mode */
  2376. if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2377. (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
  2378. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2379. ret = -EINVAL;
  2380. goto out;
  2381. }
  2382. /* allow to reduce meta or sys integrity only if force set */
  2383. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2384. BTRFS_BLOCK_GROUP_RAID10;
  2385. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2386. (fs_info->avail_system_alloc_bits & allowed) &&
  2387. !(bctl->sys.target & allowed)) ||
  2388. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2389. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2390. !(bctl->meta.target & allowed))) {
  2391. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2392. printk(KERN_INFO "btrfs: force reducing metadata "
  2393. "integrity\n");
  2394. } else {
  2395. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2396. "integrity, use force if you want this\n");
  2397. ret = -EINVAL;
  2398. goto out;
  2399. }
  2400. }
  2401. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2402. int num_tolerated_disk_barrier_failures;
  2403. u64 target = bctl->sys.target;
  2404. num_tolerated_disk_barrier_failures =
  2405. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2406. if (num_tolerated_disk_barrier_failures > 0 &&
  2407. (target &
  2408. (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  2409. BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
  2410. num_tolerated_disk_barrier_failures = 0;
  2411. else if (num_tolerated_disk_barrier_failures > 1 &&
  2412. (target &
  2413. (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
  2414. num_tolerated_disk_barrier_failures = 1;
  2415. fs_info->num_tolerated_disk_barrier_failures =
  2416. num_tolerated_disk_barrier_failures;
  2417. }
  2418. ret = insert_balance_item(fs_info->tree_root, bctl);
  2419. if (ret && ret != -EEXIST)
  2420. goto out;
  2421. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2422. BUG_ON(ret == -EEXIST);
  2423. set_balance_control(bctl);
  2424. } else {
  2425. BUG_ON(ret != -EEXIST);
  2426. spin_lock(&fs_info->balance_lock);
  2427. update_balance_args(bctl);
  2428. spin_unlock(&fs_info->balance_lock);
  2429. }
  2430. atomic_inc(&fs_info->balance_running);
  2431. mutex_unlock(&fs_info->balance_mutex);
  2432. ret = __btrfs_balance(fs_info);
  2433. mutex_lock(&fs_info->balance_mutex);
  2434. atomic_dec(&fs_info->balance_running);
  2435. if (bargs) {
  2436. memset(bargs, 0, sizeof(*bargs));
  2437. update_ioctl_balance_args(fs_info, 0, bargs);
  2438. }
  2439. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  2440. balance_need_close(fs_info)) {
  2441. __cancel_balance(fs_info);
  2442. }
  2443. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2444. fs_info->num_tolerated_disk_barrier_failures =
  2445. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2446. }
  2447. wake_up(&fs_info->balance_wait_q);
  2448. return ret;
  2449. out:
  2450. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2451. __cancel_balance(fs_info);
  2452. else
  2453. kfree(bctl);
  2454. return ret;
  2455. }
  2456. static int balance_kthread(void *data)
  2457. {
  2458. struct btrfs_fs_info *fs_info = data;
  2459. int ret = 0;
  2460. mutex_lock(&fs_info->volume_mutex);
  2461. mutex_lock(&fs_info->balance_mutex);
  2462. if (fs_info->balance_ctl) {
  2463. printk(KERN_INFO "btrfs: continuing balance\n");
  2464. ret = btrfs_balance(fs_info->balance_ctl, NULL);
  2465. }
  2466. mutex_unlock(&fs_info->balance_mutex);
  2467. mutex_unlock(&fs_info->volume_mutex);
  2468. return ret;
  2469. }
  2470. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  2471. {
  2472. struct task_struct *tsk;
  2473. spin_lock(&fs_info->balance_lock);
  2474. if (!fs_info->balance_ctl) {
  2475. spin_unlock(&fs_info->balance_lock);
  2476. return 0;
  2477. }
  2478. spin_unlock(&fs_info->balance_lock);
  2479. if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
  2480. printk(KERN_INFO "btrfs: force skipping balance\n");
  2481. return 0;
  2482. }
  2483. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  2484. if (IS_ERR(tsk))
  2485. return PTR_ERR(tsk);
  2486. return 0;
  2487. }
  2488. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  2489. {
  2490. struct btrfs_balance_control *bctl;
  2491. struct btrfs_balance_item *item;
  2492. struct btrfs_disk_balance_args disk_bargs;
  2493. struct btrfs_path *path;
  2494. struct extent_buffer *leaf;
  2495. struct btrfs_key key;
  2496. int ret;
  2497. path = btrfs_alloc_path();
  2498. if (!path)
  2499. return -ENOMEM;
  2500. key.objectid = BTRFS_BALANCE_OBJECTID;
  2501. key.type = BTRFS_BALANCE_ITEM_KEY;
  2502. key.offset = 0;
  2503. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  2504. if (ret < 0)
  2505. goto out;
  2506. if (ret > 0) { /* ret = -ENOENT; */
  2507. ret = 0;
  2508. goto out;
  2509. }
  2510. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2511. if (!bctl) {
  2512. ret = -ENOMEM;
  2513. goto out;
  2514. }
  2515. leaf = path->nodes[0];
  2516. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2517. bctl->fs_info = fs_info;
  2518. bctl->flags = btrfs_balance_flags(leaf, item);
  2519. bctl->flags |= BTRFS_BALANCE_RESUME;
  2520. btrfs_balance_data(leaf, item, &disk_bargs);
  2521. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2522. btrfs_balance_meta(leaf, item, &disk_bargs);
  2523. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2524. btrfs_balance_sys(leaf, item, &disk_bargs);
  2525. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2526. mutex_lock(&fs_info->volume_mutex);
  2527. mutex_lock(&fs_info->balance_mutex);
  2528. set_balance_control(bctl);
  2529. mutex_unlock(&fs_info->balance_mutex);
  2530. mutex_unlock(&fs_info->volume_mutex);
  2531. out:
  2532. btrfs_free_path(path);
  2533. return ret;
  2534. }
  2535. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  2536. {
  2537. int ret = 0;
  2538. mutex_lock(&fs_info->balance_mutex);
  2539. if (!fs_info->balance_ctl) {
  2540. mutex_unlock(&fs_info->balance_mutex);
  2541. return -ENOTCONN;
  2542. }
  2543. if (atomic_read(&fs_info->balance_running)) {
  2544. atomic_inc(&fs_info->balance_pause_req);
  2545. mutex_unlock(&fs_info->balance_mutex);
  2546. wait_event(fs_info->balance_wait_q,
  2547. atomic_read(&fs_info->balance_running) == 0);
  2548. mutex_lock(&fs_info->balance_mutex);
  2549. /* we are good with balance_ctl ripped off from under us */
  2550. BUG_ON(atomic_read(&fs_info->balance_running));
  2551. atomic_dec(&fs_info->balance_pause_req);
  2552. } else {
  2553. ret = -ENOTCONN;
  2554. }
  2555. mutex_unlock(&fs_info->balance_mutex);
  2556. return ret;
  2557. }
  2558. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  2559. {
  2560. mutex_lock(&fs_info->balance_mutex);
  2561. if (!fs_info->balance_ctl) {
  2562. mutex_unlock(&fs_info->balance_mutex);
  2563. return -ENOTCONN;
  2564. }
  2565. atomic_inc(&fs_info->balance_cancel_req);
  2566. /*
  2567. * if we are running just wait and return, balance item is
  2568. * deleted in btrfs_balance in this case
  2569. */
  2570. if (atomic_read(&fs_info->balance_running)) {
  2571. mutex_unlock(&fs_info->balance_mutex);
  2572. wait_event(fs_info->balance_wait_q,
  2573. atomic_read(&fs_info->balance_running) == 0);
  2574. mutex_lock(&fs_info->balance_mutex);
  2575. } else {
  2576. /* __cancel_balance needs volume_mutex */
  2577. mutex_unlock(&fs_info->balance_mutex);
  2578. mutex_lock(&fs_info->volume_mutex);
  2579. mutex_lock(&fs_info->balance_mutex);
  2580. if (fs_info->balance_ctl)
  2581. __cancel_balance(fs_info);
  2582. mutex_unlock(&fs_info->volume_mutex);
  2583. }
  2584. BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
  2585. atomic_dec(&fs_info->balance_cancel_req);
  2586. mutex_unlock(&fs_info->balance_mutex);
  2587. return 0;
  2588. }
  2589. /*
  2590. * shrinking a device means finding all of the device extents past
  2591. * the new size, and then following the back refs to the chunks.
  2592. * The chunk relocation code actually frees the device extent
  2593. */
  2594. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2595. {
  2596. struct btrfs_trans_handle *trans;
  2597. struct btrfs_root *root = device->dev_root;
  2598. struct btrfs_dev_extent *dev_extent = NULL;
  2599. struct btrfs_path *path;
  2600. u64 length;
  2601. u64 chunk_tree;
  2602. u64 chunk_objectid;
  2603. u64 chunk_offset;
  2604. int ret;
  2605. int slot;
  2606. int failed = 0;
  2607. bool retried = false;
  2608. struct extent_buffer *l;
  2609. struct btrfs_key key;
  2610. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2611. u64 old_total = btrfs_super_total_bytes(super_copy);
  2612. u64 old_size = device->total_bytes;
  2613. u64 diff = device->total_bytes - new_size;
  2614. path = btrfs_alloc_path();
  2615. if (!path)
  2616. return -ENOMEM;
  2617. path->reada = 2;
  2618. lock_chunks(root);
  2619. device->total_bytes = new_size;
  2620. if (device->writeable) {
  2621. device->fs_devices->total_rw_bytes -= diff;
  2622. spin_lock(&root->fs_info->free_chunk_lock);
  2623. root->fs_info->free_chunk_space -= diff;
  2624. spin_unlock(&root->fs_info->free_chunk_lock);
  2625. }
  2626. unlock_chunks(root);
  2627. again:
  2628. key.objectid = device->devid;
  2629. key.offset = (u64)-1;
  2630. key.type = BTRFS_DEV_EXTENT_KEY;
  2631. do {
  2632. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2633. if (ret < 0)
  2634. goto done;
  2635. ret = btrfs_previous_item(root, path, 0, key.type);
  2636. if (ret < 0)
  2637. goto done;
  2638. if (ret) {
  2639. ret = 0;
  2640. btrfs_release_path(path);
  2641. break;
  2642. }
  2643. l = path->nodes[0];
  2644. slot = path->slots[0];
  2645. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2646. if (key.objectid != device->devid) {
  2647. btrfs_release_path(path);
  2648. break;
  2649. }
  2650. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2651. length = btrfs_dev_extent_length(l, dev_extent);
  2652. if (key.offset + length <= new_size) {
  2653. btrfs_release_path(path);
  2654. break;
  2655. }
  2656. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2657. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2658. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2659. btrfs_release_path(path);
  2660. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2661. chunk_offset);
  2662. if (ret && ret != -ENOSPC)
  2663. goto done;
  2664. if (ret == -ENOSPC)
  2665. failed++;
  2666. } while (key.offset-- > 0);
  2667. if (failed && !retried) {
  2668. failed = 0;
  2669. retried = true;
  2670. goto again;
  2671. } else if (failed && retried) {
  2672. ret = -ENOSPC;
  2673. lock_chunks(root);
  2674. device->total_bytes = old_size;
  2675. if (device->writeable)
  2676. device->fs_devices->total_rw_bytes += diff;
  2677. spin_lock(&root->fs_info->free_chunk_lock);
  2678. root->fs_info->free_chunk_space += diff;
  2679. spin_unlock(&root->fs_info->free_chunk_lock);
  2680. unlock_chunks(root);
  2681. goto done;
  2682. }
  2683. /* Shrinking succeeded, else we would be at "done". */
  2684. trans = btrfs_start_transaction(root, 0);
  2685. if (IS_ERR(trans)) {
  2686. ret = PTR_ERR(trans);
  2687. goto done;
  2688. }
  2689. lock_chunks(root);
  2690. device->disk_total_bytes = new_size;
  2691. /* Now btrfs_update_device() will change the on-disk size. */
  2692. ret = btrfs_update_device(trans, device);
  2693. if (ret) {
  2694. unlock_chunks(root);
  2695. btrfs_end_transaction(trans, root);
  2696. goto done;
  2697. }
  2698. WARN_ON(diff > old_total);
  2699. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  2700. unlock_chunks(root);
  2701. btrfs_end_transaction(trans, root);
  2702. done:
  2703. btrfs_free_path(path);
  2704. return ret;
  2705. }
  2706. static int btrfs_add_system_chunk(struct btrfs_root *root,
  2707. struct btrfs_key *key,
  2708. struct btrfs_chunk *chunk, int item_size)
  2709. {
  2710. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2711. struct btrfs_disk_key disk_key;
  2712. u32 array_size;
  2713. u8 *ptr;
  2714. array_size = btrfs_super_sys_array_size(super_copy);
  2715. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2716. return -EFBIG;
  2717. ptr = super_copy->sys_chunk_array + array_size;
  2718. btrfs_cpu_key_to_disk(&disk_key, key);
  2719. memcpy(ptr, &disk_key, sizeof(disk_key));
  2720. ptr += sizeof(disk_key);
  2721. memcpy(ptr, chunk, item_size);
  2722. item_size += sizeof(disk_key);
  2723. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2724. return 0;
  2725. }
  2726. /*
  2727. * sort the devices in descending order by max_avail, total_avail
  2728. */
  2729. static int btrfs_cmp_device_info(const void *a, const void *b)
  2730. {
  2731. const struct btrfs_device_info *di_a = a;
  2732. const struct btrfs_device_info *di_b = b;
  2733. if (di_a->max_avail > di_b->max_avail)
  2734. return -1;
  2735. if (di_a->max_avail < di_b->max_avail)
  2736. return 1;
  2737. if (di_a->total_avail > di_b->total_avail)
  2738. return -1;
  2739. if (di_a->total_avail < di_b->total_avail)
  2740. return 1;
  2741. return 0;
  2742. }
  2743. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2744. struct btrfs_root *extent_root,
  2745. struct map_lookup **map_ret,
  2746. u64 *num_bytes_out, u64 *stripe_size_out,
  2747. u64 start, u64 type)
  2748. {
  2749. struct btrfs_fs_info *info = extent_root->fs_info;
  2750. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2751. struct list_head *cur;
  2752. struct map_lookup *map = NULL;
  2753. struct extent_map_tree *em_tree;
  2754. struct extent_map *em;
  2755. struct btrfs_device_info *devices_info = NULL;
  2756. u64 total_avail;
  2757. int num_stripes; /* total number of stripes to allocate */
  2758. int sub_stripes; /* sub_stripes info for map */
  2759. int dev_stripes; /* stripes per dev */
  2760. int devs_max; /* max devs to use */
  2761. int devs_min; /* min devs needed */
  2762. int devs_increment; /* ndevs has to be a multiple of this */
  2763. int ncopies; /* how many copies to data has */
  2764. int ret;
  2765. u64 max_stripe_size;
  2766. u64 max_chunk_size;
  2767. u64 stripe_size;
  2768. u64 num_bytes;
  2769. int ndevs;
  2770. int i;
  2771. int j;
  2772. BUG_ON(!alloc_profile_is_valid(type, 0));
  2773. if (list_empty(&fs_devices->alloc_list))
  2774. return -ENOSPC;
  2775. sub_stripes = 1;
  2776. dev_stripes = 1;
  2777. devs_increment = 1;
  2778. ncopies = 1;
  2779. devs_max = 0; /* 0 == as many as possible */
  2780. devs_min = 1;
  2781. /*
  2782. * define the properties of each RAID type.
  2783. * FIXME: move this to a global table and use it in all RAID
  2784. * calculation code
  2785. */
  2786. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2787. dev_stripes = 2;
  2788. ncopies = 2;
  2789. devs_max = 1;
  2790. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2791. devs_min = 2;
  2792. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2793. devs_increment = 2;
  2794. ncopies = 2;
  2795. devs_max = 2;
  2796. devs_min = 2;
  2797. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2798. sub_stripes = 2;
  2799. devs_increment = 2;
  2800. ncopies = 2;
  2801. devs_min = 4;
  2802. } else {
  2803. devs_max = 1;
  2804. }
  2805. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2806. max_stripe_size = 1024 * 1024 * 1024;
  2807. max_chunk_size = 10 * max_stripe_size;
  2808. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2809. /* for larger filesystems, use larger metadata chunks */
  2810. if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
  2811. max_stripe_size = 1024 * 1024 * 1024;
  2812. else
  2813. max_stripe_size = 256 * 1024 * 1024;
  2814. max_chunk_size = max_stripe_size;
  2815. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2816. max_stripe_size = 32 * 1024 * 1024;
  2817. max_chunk_size = 2 * max_stripe_size;
  2818. } else {
  2819. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2820. type);
  2821. BUG_ON(1);
  2822. }
  2823. /* we don't want a chunk larger than 10% of writeable space */
  2824. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2825. max_chunk_size);
  2826. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2827. GFP_NOFS);
  2828. if (!devices_info)
  2829. return -ENOMEM;
  2830. cur = fs_devices->alloc_list.next;
  2831. /*
  2832. * in the first pass through the devices list, we gather information
  2833. * about the available holes on each device.
  2834. */
  2835. ndevs = 0;
  2836. while (cur != &fs_devices->alloc_list) {
  2837. struct btrfs_device *device;
  2838. u64 max_avail;
  2839. u64 dev_offset;
  2840. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2841. cur = cur->next;
  2842. if (!device->writeable) {
  2843. printk(KERN_ERR
  2844. "btrfs: read-only device in alloc_list\n");
  2845. WARN_ON(1);
  2846. continue;
  2847. }
  2848. if (!device->in_fs_metadata)
  2849. continue;
  2850. if (device->total_bytes > device->bytes_used)
  2851. total_avail = device->total_bytes - device->bytes_used;
  2852. else
  2853. total_avail = 0;
  2854. /* If there is no space on this device, skip it. */
  2855. if (total_avail == 0)
  2856. continue;
  2857. ret = find_free_dev_extent(device,
  2858. max_stripe_size * dev_stripes,
  2859. &dev_offset, &max_avail);
  2860. if (ret && ret != -ENOSPC)
  2861. goto error;
  2862. if (ret == 0)
  2863. max_avail = max_stripe_size * dev_stripes;
  2864. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2865. continue;
  2866. devices_info[ndevs].dev_offset = dev_offset;
  2867. devices_info[ndevs].max_avail = max_avail;
  2868. devices_info[ndevs].total_avail = total_avail;
  2869. devices_info[ndevs].dev = device;
  2870. ++ndevs;
  2871. }
  2872. /*
  2873. * now sort the devices by hole size / available space
  2874. */
  2875. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2876. btrfs_cmp_device_info, NULL);
  2877. /* round down to number of usable stripes */
  2878. ndevs -= ndevs % devs_increment;
  2879. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2880. ret = -ENOSPC;
  2881. goto error;
  2882. }
  2883. if (devs_max && ndevs > devs_max)
  2884. ndevs = devs_max;
  2885. /*
  2886. * the primary goal is to maximize the number of stripes, so use as many
  2887. * devices as possible, even if the stripes are not maximum sized.
  2888. */
  2889. stripe_size = devices_info[ndevs-1].max_avail;
  2890. num_stripes = ndevs * dev_stripes;
  2891. if (stripe_size * ndevs > max_chunk_size * ncopies) {
  2892. stripe_size = max_chunk_size * ncopies;
  2893. do_div(stripe_size, ndevs);
  2894. }
  2895. do_div(stripe_size, dev_stripes);
  2896. /* align to BTRFS_STRIPE_LEN */
  2897. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2898. stripe_size *= BTRFS_STRIPE_LEN;
  2899. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2900. if (!map) {
  2901. ret = -ENOMEM;
  2902. goto error;
  2903. }
  2904. map->num_stripes = num_stripes;
  2905. for (i = 0; i < ndevs; ++i) {
  2906. for (j = 0; j < dev_stripes; ++j) {
  2907. int s = i * dev_stripes + j;
  2908. map->stripes[s].dev = devices_info[i].dev;
  2909. map->stripes[s].physical = devices_info[i].dev_offset +
  2910. j * stripe_size;
  2911. }
  2912. }
  2913. map->sector_size = extent_root->sectorsize;
  2914. map->stripe_len = BTRFS_STRIPE_LEN;
  2915. map->io_align = BTRFS_STRIPE_LEN;
  2916. map->io_width = BTRFS_STRIPE_LEN;
  2917. map->type = type;
  2918. map->sub_stripes = sub_stripes;
  2919. *map_ret = map;
  2920. num_bytes = stripe_size * (num_stripes / ncopies);
  2921. *stripe_size_out = stripe_size;
  2922. *num_bytes_out = num_bytes;
  2923. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2924. em = alloc_extent_map();
  2925. if (!em) {
  2926. ret = -ENOMEM;
  2927. goto error;
  2928. }
  2929. em->bdev = (struct block_device *)map;
  2930. em->start = start;
  2931. em->len = num_bytes;
  2932. em->block_start = 0;
  2933. em->block_len = em->len;
  2934. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2935. write_lock(&em_tree->lock);
  2936. ret = add_extent_mapping(em_tree, em);
  2937. write_unlock(&em_tree->lock);
  2938. free_extent_map(em);
  2939. if (ret)
  2940. goto error;
  2941. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2942. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2943. start, num_bytes);
  2944. if (ret)
  2945. goto error;
  2946. for (i = 0; i < map->num_stripes; ++i) {
  2947. struct btrfs_device *device;
  2948. u64 dev_offset;
  2949. device = map->stripes[i].dev;
  2950. dev_offset = map->stripes[i].physical;
  2951. ret = btrfs_alloc_dev_extent(trans, device,
  2952. info->chunk_root->root_key.objectid,
  2953. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2954. start, dev_offset, stripe_size);
  2955. if (ret) {
  2956. btrfs_abort_transaction(trans, extent_root, ret);
  2957. goto error;
  2958. }
  2959. }
  2960. kfree(devices_info);
  2961. return 0;
  2962. error:
  2963. kfree(map);
  2964. kfree(devices_info);
  2965. return ret;
  2966. }
  2967. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2968. struct btrfs_root *extent_root,
  2969. struct map_lookup *map, u64 chunk_offset,
  2970. u64 chunk_size, u64 stripe_size)
  2971. {
  2972. u64 dev_offset;
  2973. struct btrfs_key key;
  2974. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2975. struct btrfs_device *device;
  2976. struct btrfs_chunk *chunk;
  2977. struct btrfs_stripe *stripe;
  2978. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2979. int index = 0;
  2980. int ret;
  2981. chunk = kzalloc(item_size, GFP_NOFS);
  2982. if (!chunk)
  2983. return -ENOMEM;
  2984. index = 0;
  2985. while (index < map->num_stripes) {
  2986. device = map->stripes[index].dev;
  2987. device->bytes_used += stripe_size;
  2988. ret = btrfs_update_device(trans, device);
  2989. if (ret)
  2990. goto out_free;
  2991. index++;
  2992. }
  2993. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2994. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2995. map->num_stripes);
  2996. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2997. index = 0;
  2998. stripe = &chunk->stripe;
  2999. while (index < map->num_stripes) {
  3000. device = map->stripes[index].dev;
  3001. dev_offset = map->stripes[index].physical;
  3002. btrfs_set_stack_stripe_devid(stripe, device->devid);
  3003. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  3004. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  3005. stripe++;
  3006. index++;
  3007. }
  3008. btrfs_set_stack_chunk_length(chunk, chunk_size);
  3009. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  3010. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  3011. btrfs_set_stack_chunk_type(chunk, map->type);
  3012. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  3013. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  3014. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  3015. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  3016. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  3017. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3018. key.type = BTRFS_CHUNK_ITEM_KEY;
  3019. key.offset = chunk_offset;
  3020. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  3021. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3022. /*
  3023. * TODO: Cleanup of inserted chunk root in case of
  3024. * failure.
  3025. */
  3026. ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
  3027. item_size);
  3028. }
  3029. out_free:
  3030. kfree(chunk);
  3031. return ret;
  3032. }
  3033. /*
  3034. * Chunk allocation falls into two parts. The first part does works
  3035. * that make the new allocated chunk useable, but not do any operation
  3036. * that modifies the chunk tree. The second part does the works that
  3037. * require modifying the chunk tree. This division is important for the
  3038. * bootstrap process of adding storage to a seed btrfs.
  3039. */
  3040. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3041. struct btrfs_root *extent_root, u64 type)
  3042. {
  3043. u64 chunk_offset;
  3044. u64 chunk_size;
  3045. u64 stripe_size;
  3046. struct map_lookup *map;
  3047. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3048. int ret;
  3049. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3050. &chunk_offset);
  3051. if (ret)
  3052. return ret;
  3053. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3054. &stripe_size, chunk_offset, type);
  3055. if (ret)
  3056. return ret;
  3057. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3058. chunk_size, stripe_size);
  3059. if (ret)
  3060. return ret;
  3061. return 0;
  3062. }
  3063. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  3064. struct btrfs_root *root,
  3065. struct btrfs_device *device)
  3066. {
  3067. u64 chunk_offset;
  3068. u64 sys_chunk_offset;
  3069. u64 chunk_size;
  3070. u64 sys_chunk_size;
  3071. u64 stripe_size;
  3072. u64 sys_stripe_size;
  3073. u64 alloc_profile;
  3074. struct map_lookup *map;
  3075. struct map_lookup *sys_map;
  3076. struct btrfs_fs_info *fs_info = root->fs_info;
  3077. struct btrfs_root *extent_root = fs_info->extent_root;
  3078. int ret;
  3079. ret = find_next_chunk(fs_info->chunk_root,
  3080. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  3081. if (ret)
  3082. return ret;
  3083. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  3084. fs_info->avail_metadata_alloc_bits;
  3085. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  3086. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3087. &stripe_size, chunk_offset, alloc_profile);
  3088. if (ret)
  3089. return ret;
  3090. sys_chunk_offset = chunk_offset + chunk_size;
  3091. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  3092. fs_info->avail_system_alloc_bits;
  3093. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  3094. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  3095. &sys_chunk_size, &sys_stripe_size,
  3096. sys_chunk_offset, alloc_profile);
  3097. if (ret) {
  3098. btrfs_abort_transaction(trans, root, ret);
  3099. goto out;
  3100. }
  3101. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  3102. if (ret) {
  3103. btrfs_abort_transaction(trans, root, ret);
  3104. goto out;
  3105. }
  3106. /*
  3107. * Modifying chunk tree needs allocating new blocks from both
  3108. * system block group and metadata block group. So we only can
  3109. * do operations require modifying the chunk tree after both
  3110. * block groups were created.
  3111. */
  3112. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3113. chunk_size, stripe_size);
  3114. if (ret) {
  3115. btrfs_abort_transaction(trans, root, ret);
  3116. goto out;
  3117. }
  3118. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  3119. sys_chunk_offset, sys_chunk_size,
  3120. sys_stripe_size);
  3121. if (ret)
  3122. btrfs_abort_transaction(trans, root, ret);
  3123. out:
  3124. return ret;
  3125. }
  3126. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  3127. {
  3128. struct extent_map *em;
  3129. struct map_lookup *map;
  3130. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3131. int readonly = 0;
  3132. int i;
  3133. read_lock(&map_tree->map_tree.lock);
  3134. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  3135. read_unlock(&map_tree->map_tree.lock);
  3136. if (!em)
  3137. return 1;
  3138. if (btrfs_test_opt(root, DEGRADED)) {
  3139. free_extent_map(em);
  3140. return 0;
  3141. }
  3142. map = (struct map_lookup *)em->bdev;
  3143. for (i = 0; i < map->num_stripes; i++) {
  3144. if (!map->stripes[i].dev->writeable) {
  3145. readonly = 1;
  3146. break;
  3147. }
  3148. }
  3149. free_extent_map(em);
  3150. return readonly;
  3151. }
  3152. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  3153. {
  3154. extent_map_tree_init(&tree->map_tree);
  3155. }
  3156. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  3157. {
  3158. struct extent_map *em;
  3159. while (1) {
  3160. write_lock(&tree->map_tree.lock);
  3161. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  3162. if (em)
  3163. remove_extent_mapping(&tree->map_tree, em);
  3164. write_unlock(&tree->map_tree.lock);
  3165. if (!em)
  3166. break;
  3167. kfree(em->bdev);
  3168. /* once for us */
  3169. free_extent_map(em);
  3170. /* once for the tree */
  3171. free_extent_map(em);
  3172. }
  3173. }
  3174. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  3175. {
  3176. struct extent_map *em;
  3177. struct map_lookup *map;
  3178. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3179. int ret;
  3180. read_lock(&em_tree->lock);
  3181. em = lookup_extent_mapping(em_tree, logical, len);
  3182. read_unlock(&em_tree->lock);
  3183. BUG_ON(!em);
  3184. BUG_ON(em->start > logical || em->start + em->len < logical);
  3185. map = (struct map_lookup *)em->bdev;
  3186. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  3187. ret = map->num_stripes;
  3188. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3189. ret = map->sub_stripes;
  3190. else
  3191. ret = 1;
  3192. free_extent_map(em);
  3193. return ret;
  3194. }
  3195. static int find_live_mirror(struct map_lookup *map, int first, int num,
  3196. int optimal)
  3197. {
  3198. int i;
  3199. if (map->stripes[optimal].dev->bdev)
  3200. return optimal;
  3201. for (i = first; i < first + num; i++) {
  3202. if (map->stripes[i].dev->bdev)
  3203. return i;
  3204. }
  3205. /* we couldn't find one that doesn't fail. Just return something
  3206. * and the io error handling code will clean up eventually
  3207. */
  3208. return optimal;
  3209. }
  3210. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  3211. u64 logical, u64 *length,
  3212. struct btrfs_bio **bbio_ret,
  3213. int mirror_num)
  3214. {
  3215. struct extent_map *em;
  3216. struct map_lookup *map;
  3217. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3218. u64 offset;
  3219. u64 stripe_offset;
  3220. u64 stripe_end_offset;
  3221. u64 stripe_nr;
  3222. u64 stripe_nr_orig;
  3223. u64 stripe_nr_end;
  3224. int stripe_index;
  3225. int i;
  3226. int ret = 0;
  3227. int num_stripes;
  3228. int max_errors = 0;
  3229. struct btrfs_bio *bbio = NULL;
  3230. read_lock(&em_tree->lock);
  3231. em = lookup_extent_mapping(em_tree, logical, *length);
  3232. read_unlock(&em_tree->lock);
  3233. if (!em) {
  3234. printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
  3235. (unsigned long long)logical,
  3236. (unsigned long long)*length);
  3237. BUG();
  3238. }
  3239. BUG_ON(em->start > logical || em->start + em->len < logical);
  3240. map = (struct map_lookup *)em->bdev;
  3241. offset = logical - em->start;
  3242. if (mirror_num > map->num_stripes)
  3243. mirror_num = 0;
  3244. stripe_nr = offset;
  3245. /*
  3246. * stripe_nr counts the total number of stripes we have to stride
  3247. * to get to this block
  3248. */
  3249. do_div(stripe_nr, map->stripe_len);
  3250. stripe_offset = stripe_nr * map->stripe_len;
  3251. BUG_ON(offset < stripe_offset);
  3252. /* stripe_offset is the offset of this block in its stripe*/
  3253. stripe_offset = offset - stripe_offset;
  3254. if (rw & REQ_DISCARD)
  3255. *length = min_t(u64, em->len - offset, *length);
  3256. else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  3257. /* we limit the length of each bio to what fits in a stripe */
  3258. *length = min_t(u64, em->len - offset,
  3259. map->stripe_len - stripe_offset);
  3260. } else {
  3261. *length = em->len - offset;
  3262. }
  3263. if (!bbio_ret)
  3264. goto out;
  3265. num_stripes = 1;
  3266. stripe_index = 0;
  3267. stripe_nr_orig = stripe_nr;
  3268. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  3269. (~(map->stripe_len - 1));
  3270. do_div(stripe_nr_end, map->stripe_len);
  3271. stripe_end_offset = stripe_nr_end * map->stripe_len -
  3272. (offset + *length);
  3273. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3274. if (rw & REQ_DISCARD)
  3275. num_stripes = min_t(u64, map->num_stripes,
  3276. stripe_nr_end - stripe_nr_orig);
  3277. stripe_index = do_div(stripe_nr, map->num_stripes);
  3278. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  3279. if (rw & (REQ_WRITE | REQ_DISCARD))
  3280. num_stripes = map->num_stripes;
  3281. else if (mirror_num)
  3282. stripe_index = mirror_num - 1;
  3283. else {
  3284. stripe_index = find_live_mirror(map, 0,
  3285. map->num_stripes,
  3286. current->pid % map->num_stripes);
  3287. mirror_num = stripe_index + 1;
  3288. }
  3289. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  3290. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  3291. num_stripes = map->num_stripes;
  3292. } else if (mirror_num) {
  3293. stripe_index = mirror_num - 1;
  3294. } else {
  3295. mirror_num = 1;
  3296. }
  3297. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3298. int factor = map->num_stripes / map->sub_stripes;
  3299. stripe_index = do_div(stripe_nr, factor);
  3300. stripe_index *= map->sub_stripes;
  3301. if (rw & REQ_WRITE)
  3302. num_stripes = map->sub_stripes;
  3303. else if (rw & REQ_DISCARD)
  3304. num_stripes = min_t(u64, map->sub_stripes *
  3305. (stripe_nr_end - stripe_nr_orig),
  3306. map->num_stripes);
  3307. else if (mirror_num)
  3308. stripe_index += mirror_num - 1;
  3309. else {
  3310. int old_stripe_index = stripe_index;
  3311. stripe_index = find_live_mirror(map, stripe_index,
  3312. map->sub_stripes, stripe_index +
  3313. current->pid % map->sub_stripes);
  3314. mirror_num = stripe_index - old_stripe_index + 1;
  3315. }
  3316. } else {
  3317. /*
  3318. * after this do_div call, stripe_nr is the number of stripes
  3319. * on this device we have to walk to find the data, and
  3320. * stripe_index is the number of our device in the stripe array
  3321. */
  3322. stripe_index = do_div(stripe_nr, map->num_stripes);
  3323. mirror_num = stripe_index + 1;
  3324. }
  3325. BUG_ON(stripe_index >= map->num_stripes);
  3326. bbio = kzalloc(btrfs_bio_size(num_stripes), GFP_NOFS);
  3327. if (!bbio) {
  3328. ret = -ENOMEM;
  3329. goto out;
  3330. }
  3331. atomic_set(&bbio->error, 0);
  3332. if (rw & REQ_DISCARD) {
  3333. int factor = 0;
  3334. int sub_stripes = 0;
  3335. u64 stripes_per_dev = 0;
  3336. u32 remaining_stripes = 0;
  3337. u32 last_stripe = 0;
  3338. if (map->type &
  3339. (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
  3340. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3341. sub_stripes = 1;
  3342. else
  3343. sub_stripes = map->sub_stripes;
  3344. factor = map->num_stripes / sub_stripes;
  3345. stripes_per_dev = div_u64_rem(stripe_nr_end -
  3346. stripe_nr_orig,
  3347. factor,
  3348. &remaining_stripes);
  3349. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  3350. last_stripe *= sub_stripes;
  3351. }
  3352. for (i = 0; i < num_stripes; i++) {
  3353. bbio->stripes[i].physical =
  3354. map->stripes[stripe_index].physical +
  3355. stripe_offset + stripe_nr * map->stripe_len;
  3356. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  3357. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  3358. BTRFS_BLOCK_GROUP_RAID10)) {
  3359. bbio->stripes[i].length = stripes_per_dev *
  3360. map->stripe_len;
  3361. if (i / sub_stripes < remaining_stripes)
  3362. bbio->stripes[i].length +=
  3363. map->stripe_len;
  3364. /*
  3365. * Special for the first stripe and
  3366. * the last stripe:
  3367. *
  3368. * |-------|...|-------|
  3369. * |----------|
  3370. * off end_off
  3371. */
  3372. if (i < sub_stripes)
  3373. bbio->stripes[i].length -=
  3374. stripe_offset;
  3375. if (stripe_index >= last_stripe &&
  3376. stripe_index <= (last_stripe +
  3377. sub_stripes - 1))
  3378. bbio->stripes[i].length -=
  3379. stripe_end_offset;
  3380. if (i == sub_stripes - 1)
  3381. stripe_offset = 0;
  3382. } else
  3383. bbio->stripes[i].length = *length;
  3384. stripe_index++;
  3385. if (stripe_index == map->num_stripes) {
  3386. /* This could only happen for RAID0/10 */
  3387. stripe_index = 0;
  3388. stripe_nr++;
  3389. }
  3390. }
  3391. } else {
  3392. for (i = 0; i < num_stripes; i++) {
  3393. bbio->stripes[i].physical =
  3394. map->stripes[stripe_index].physical +
  3395. stripe_offset +
  3396. stripe_nr * map->stripe_len;
  3397. bbio->stripes[i].dev =
  3398. map->stripes[stripe_index].dev;
  3399. stripe_index++;
  3400. }
  3401. }
  3402. if (rw & REQ_WRITE) {
  3403. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  3404. BTRFS_BLOCK_GROUP_RAID10 |
  3405. BTRFS_BLOCK_GROUP_DUP)) {
  3406. max_errors = 1;
  3407. }
  3408. }
  3409. *bbio_ret = bbio;
  3410. bbio->num_stripes = num_stripes;
  3411. bbio->max_errors = max_errors;
  3412. bbio->mirror_num = mirror_num;
  3413. out:
  3414. free_extent_map(em);
  3415. return ret;
  3416. }
  3417. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  3418. u64 logical, u64 *length,
  3419. struct btrfs_bio **bbio_ret, int mirror_num)
  3420. {
  3421. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  3422. mirror_num);
  3423. }
  3424. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  3425. u64 chunk_start, u64 physical, u64 devid,
  3426. u64 **logical, int *naddrs, int *stripe_len)
  3427. {
  3428. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3429. struct extent_map *em;
  3430. struct map_lookup *map;
  3431. u64 *buf;
  3432. u64 bytenr;
  3433. u64 length;
  3434. u64 stripe_nr;
  3435. int i, j, nr = 0;
  3436. read_lock(&em_tree->lock);
  3437. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  3438. read_unlock(&em_tree->lock);
  3439. BUG_ON(!em || em->start != chunk_start);
  3440. map = (struct map_lookup *)em->bdev;
  3441. length = em->len;
  3442. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3443. do_div(length, map->num_stripes / map->sub_stripes);
  3444. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3445. do_div(length, map->num_stripes);
  3446. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  3447. BUG_ON(!buf); /* -ENOMEM */
  3448. for (i = 0; i < map->num_stripes; i++) {
  3449. if (devid && map->stripes[i].dev->devid != devid)
  3450. continue;
  3451. if (map->stripes[i].physical > physical ||
  3452. map->stripes[i].physical + length <= physical)
  3453. continue;
  3454. stripe_nr = physical - map->stripes[i].physical;
  3455. do_div(stripe_nr, map->stripe_len);
  3456. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3457. stripe_nr = stripe_nr * map->num_stripes + i;
  3458. do_div(stripe_nr, map->sub_stripes);
  3459. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3460. stripe_nr = stripe_nr * map->num_stripes + i;
  3461. }
  3462. bytenr = chunk_start + stripe_nr * map->stripe_len;
  3463. WARN_ON(nr >= map->num_stripes);
  3464. for (j = 0; j < nr; j++) {
  3465. if (buf[j] == bytenr)
  3466. break;
  3467. }
  3468. if (j == nr) {
  3469. WARN_ON(nr >= map->num_stripes);
  3470. buf[nr++] = bytenr;
  3471. }
  3472. }
  3473. *logical = buf;
  3474. *naddrs = nr;
  3475. *stripe_len = map->stripe_len;
  3476. free_extent_map(em);
  3477. return 0;
  3478. }
  3479. static void *merge_stripe_index_into_bio_private(void *bi_private,
  3480. unsigned int stripe_index)
  3481. {
  3482. /*
  3483. * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
  3484. * at most 1.
  3485. * The alternative solution (instead of stealing bits from the
  3486. * pointer) would be to allocate an intermediate structure
  3487. * that contains the old private pointer plus the stripe_index.
  3488. */
  3489. BUG_ON((((uintptr_t)bi_private) & 3) != 0);
  3490. BUG_ON(stripe_index > 3);
  3491. return (void *)(((uintptr_t)bi_private) | stripe_index);
  3492. }
  3493. static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
  3494. {
  3495. return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
  3496. }
  3497. static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
  3498. {
  3499. return (unsigned int)((uintptr_t)bi_private) & 3;
  3500. }
  3501. static void btrfs_end_bio(struct bio *bio, int err)
  3502. {
  3503. struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
  3504. int is_orig_bio = 0;
  3505. if (err) {
  3506. atomic_inc(&bbio->error);
  3507. if (err == -EIO || err == -EREMOTEIO) {
  3508. unsigned int stripe_index =
  3509. extract_stripe_index_from_bio_private(
  3510. bio->bi_private);
  3511. struct btrfs_device *dev;
  3512. BUG_ON(stripe_index >= bbio->num_stripes);
  3513. dev = bbio->stripes[stripe_index].dev;
  3514. if (dev->bdev) {
  3515. if (bio->bi_rw & WRITE)
  3516. btrfs_dev_stat_inc(dev,
  3517. BTRFS_DEV_STAT_WRITE_ERRS);
  3518. else
  3519. btrfs_dev_stat_inc(dev,
  3520. BTRFS_DEV_STAT_READ_ERRS);
  3521. if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
  3522. btrfs_dev_stat_inc(dev,
  3523. BTRFS_DEV_STAT_FLUSH_ERRS);
  3524. btrfs_dev_stat_print_on_error(dev);
  3525. }
  3526. }
  3527. }
  3528. if (bio == bbio->orig_bio)
  3529. is_orig_bio = 1;
  3530. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3531. if (!is_orig_bio) {
  3532. bio_put(bio);
  3533. bio = bbio->orig_bio;
  3534. }
  3535. bio->bi_private = bbio->private;
  3536. bio->bi_end_io = bbio->end_io;
  3537. bio->bi_bdev = (struct block_device *)
  3538. (unsigned long)bbio->mirror_num;
  3539. /* only send an error to the higher layers if it is
  3540. * beyond the tolerance of the multi-bio
  3541. */
  3542. if (atomic_read(&bbio->error) > bbio->max_errors) {
  3543. err = -EIO;
  3544. } else {
  3545. /*
  3546. * this bio is actually up to date, we didn't
  3547. * go over the max number of errors
  3548. */
  3549. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3550. err = 0;
  3551. }
  3552. kfree(bbio);
  3553. bio_endio(bio, err);
  3554. } else if (!is_orig_bio) {
  3555. bio_put(bio);
  3556. }
  3557. }
  3558. struct async_sched {
  3559. struct bio *bio;
  3560. int rw;
  3561. struct btrfs_fs_info *info;
  3562. struct btrfs_work work;
  3563. };
  3564. /*
  3565. * see run_scheduled_bios for a description of why bios are collected for
  3566. * async submit.
  3567. *
  3568. * This will add one bio to the pending list for a device and make sure
  3569. * the work struct is scheduled.
  3570. */
  3571. static noinline void schedule_bio(struct btrfs_root *root,
  3572. struct btrfs_device *device,
  3573. int rw, struct bio *bio)
  3574. {
  3575. int should_queue = 1;
  3576. struct btrfs_pending_bios *pending_bios;
  3577. /* don't bother with additional async steps for reads, right now */
  3578. if (!(rw & REQ_WRITE)) {
  3579. bio_get(bio);
  3580. btrfsic_submit_bio(rw, bio);
  3581. bio_put(bio);
  3582. return;
  3583. }
  3584. /*
  3585. * nr_async_bios allows us to reliably return congestion to the
  3586. * higher layers. Otherwise, the async bio makes it appear we have
  3587. * made progress against dirty pages when we've really just put it
  3588. * on a queue for later
  3589. */
  3590. atomic_inc(&root->fs_info->nr_async_bios);
  3591. WARN_ON(bio->bi_next);
  3592. bio->bi_next = NULL;
  3593. bio->bi_rw |= rw;
  3594. spin_lock(&device->io_lock);
  3595. if (bio->bi_rw & REQ_SYNC)
  3596. pending_bios = &device->pending_sync_bios;
  3597. else
  3598. pending_bios = &device->pending_bios;
  3599. if (pending_bios->tail)
  3600. pending_bios->tail->bi_next = bio;
  3601. pending_bios->tail = bio;
  3602. if (!pending_bios->head)
  3603. pending_bios->head = bio;
  3604. if (device->running_pending)
  3605. should_queue = 0;
  3606. spin_unlock(&device->io_lock);
  3607. if (should_queue)
  3608. btrfs_queue_worker(&root->fs_info->submit_workers,
  3609. &device->work);
  3610. }
  3611. static int bio_size_ok(struct block_device *bdev, struct bio *bio,
  3612. sector_t sector)
  3613. {
  3614. struct bio_vec *prev;
  3615. struct request_queue *q = bdev_get_queue(bdev);
  3616. unsigned short max_sectors = queue_max_sectors(q);
  3617. struct bvec_merge_data bvm = {
  3618. .bi_bdev = bdev,
  3619. .bi_sector = sector,
  3620. .bi_rw = bio->bi_rw,
  3621. };
  3622. if (bio->bi_vcnt == 0) {
  3623. WARN_ON(1);
  3624. return 1;
  3625. }
  3626. prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  3627. if ((bio->bi_size >> 9) > max_sectors)
  3628. return 0;
  3629. if (!q->merge_bvec_fn)
  3630. return 1;
  3631. bvm.bi_size = bio->bi_size - prev->bv_len;
  3632. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
  3633. return 0;
  3634. return 1;
  3635. }
  3636. static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  3637. struct bio *bio, u64 physical, int dev_nr,
  3638. int rw, int async)
  3639. {
  3640. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  3641. bio->bi_private = bbio;
  3642. bio->bi_private = merge_stripe_index_into_bio_private(
  3643. bio->bi_private, (unsigned int)dev_nr);
  3644. bio->bi_end_io = btrfs_end_bio;
  3645. bio->bi_sector = physical >> 9;
  3646. #ifdef DEBUG
  3647. {
  3648. struct rcu_string *name;
  3649. rcu_read_lock();
  3650. name = rcu_dereference(dev->name);
  3651. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  3652. "(%s id %llu), size=%u\n", rw,
  3653. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  3654. name->str, dev->devid, bio->bi_size);
  3655. rcu_read_unlock();
  3656. }
  3657. #endif
  3658. bio->bi_bdev = dev->bdev;
  3659. if (async)
  3660. schedule_bio(root, dev, rw, bio);
  3661. else
  3662. btrfsic_submit_bio(rw, bio);
  3663. }
  3664. static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  3665. struct bio *first_bio, struct btrfs_device *dev,
  3666. int dev_nr, int rw, int async)
  3667. {
  3668. struct bio_vec *bvec = first_bio->bi_io_vec;
  3669. struct bio *bio;
  3670. int nr_vecs = bio_get_nr_vecs(dev->bdev);
  3671. u64 physical = bbio->stripes[dev_nr].physical;
  3672. again:
  3673. bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
  3674. if (!bio)
  3675. return -ENOMEM;
  3676. while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
  3677. if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  3678. bvec->bv_offset) < bvec->bv_len) {
  3679. u64 len = bio->bi_size;
  3680. atomic_inc(&bbio->stripes_pending);
  3681. submit_stripe_bio(root, bbio, bio, physical, dev_nr,
  3682. rw, async);
  3683. physical += len;
  3684. goto again;
  3685. }
  3686. bvec++;
  3687. }
  3688. submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
  3689. return 0;
  3690. }
  3691. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  3692. {
  3693. atomic_inc(&bbio->error);
  3694. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3695. bio->bi_private = bbio->private;
  3696. bio->bi_end_io = bbio->end_io;
  3697. bio->bi_bdev = (struct block_device *)
  3698. (unsigned long)bbio->mirror_num;
  3699. bio->bi_sector = logical >> 9;
  3700. kfree(bbio);
  3701. bio_endio(bio, -EIO);
  3702. }
  3703. }
  3704. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  3705. int mirror_num, int async_submit)
  3706. {
  3707. struct btrfs_mapping_tree *map_tree;
  3708. struct btrfs_device *dev;
  3709. struct bio *first_bio = bio;
  3710. u64 logical = (u64)bio->bi_sector << 9;
  3711. u64 length = 0;
  3712. u64 map_length;
  3713. int ret;
  3714. int dev_nr = 0;
  3715. int total_devs = 1;
  3716. struct btrfs_bio *bbio = NULL;
  3717. length = bio->bi_size;
  3718. map_tree = &root->fs_info->mapping_tree;
  3719. map_length = length;
  3720. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  3721. mirror_num);
  3722. if (ret) /* -ENOMEM */
  3723. return ret;
  3724. total_devs = bbio->num_stripes;
  3725. if (map_length < length) {
  3726. printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
  3727. "len %llu\n", (unsigned long long)logical,
  3728. (unsigned long long)length,
  3729. (unsigned long long)map_length);
  3730. BUG();
  3731. }
  3732. bbio->orig_bio = first_bio;
  3733. bbio->private = first_bio->bi_private;
  3734. bbio->end_io = first_bio->bi_end_io;
  3735. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  3736. while (dev_nr < total_devs) {
  3737. dev = bbio->stripes[dev_nr].dev;
  3738. if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
  3739. bbio_error(bbio, first_bio, logical);
  3740. dev_nr++;
  3741. continue;
  3742. }
  3743. /*
  3744. * Check and see if we're ok with this bio based on it's size
  3745. * and offset with the given device.
  3746. */
  3747. if (!bio_size_ok(dev->bdev, first_bio,
  3748. bbio->stripes[dev_nr].physical >> 9)) {
  3749. ret = breakup_stripe_bio(root, bbio, first_bio, dev,
  3750. dev_nr, rw, async_submit);
  3751. BUG_ON(ret);
  3752. dev_nr++;
  3753. continue;
  3754. }
  3755. if (dev_nr < total_devs - 1) {
  3756. bio = bio_clone(first_bio, GFP_NOFS);
  3757. BUG_ON(!bio); /* -ENOMEM */
  3758. } else {
  3759. bio = first_bio;
  3760. }
  3761. submit_stripe_bio(root, bbio, bio,
  3762. bbio->stripes[dev_nr].physical, dev_nr, rw,
  3763. async_submit);
  3764. dev_nr++;
  3765. }
  3766. return 0;
  3767. }
  3768. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  3769. u8 *uuid, u8 *fsid)
  3770. {
  3771. struct btrfs_device *device;
  3772. struct btrfs_fs_devices *cur_devices;
  3773. cur_devices = root->fs_info->fs_devices;
  3774. while (cur_devices) {
  3775. if (!fsid ||
  3776. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3777. device = __find_device(&cur_devices->devices,
  3778. devid, uuid);
  3779. if (device)
  3780. return device;
  3781. }
  3782. cur_devices = cur_devices->seed;
  3783. }
  3784. return NULL;
  3785. }
  3786. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  3787. u64 devid, u8 *dev_uuid)
  3788. {
  3789. struct btrfs_device *device;
  3790. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  3791. device = kzalloc(sizeof(*device), GFP_NOFS);
  3792. if (!device)
  3793. return NULL;
  3794. list_add(&device->dev_list,
  3795. &fs_devices->devices);
  3796. device->dev_root = root->fs_info->dev_root;
  3797. device->devid = devid;
  3798. device->work.func = pending_bios_fn;
  3799. device->fs_devices = fs_devices;
  3800. device->missing = 1;
  3801. fs_devices->num_devices++;
  3802. fs_devices->missing_devices++;
  3803. spin_lock_init(&device->io_lock);
  3804. INIT_LIST_HEAD(&device->dev_alloc_list);
  3805. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  3806. return device;
  3807. }
  3808. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  3809. struct extent_buffer *leaf,
  3810. struct btrfs_chunk *chunk)
  3811. {
  3812. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3813. struct map_lookup *map;
  3814. struct extent_map *em;
  3815. u64 logical;
  3816. u64 length;
  3817. u64 devid;
  3818. u8 uuid[BTRFS_UUID_SIZE];
  3819. int num_stripes;
  3820. int ret;
  3821. int i;
  3822. logical = key->offset;
  3823. length = btrfs_chunk_length(leaf, chunk);
  3824. read_lock(&map_tree->map_tree.lock);
  3825. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  3826. read_unlock(&map_tree->map_tree.lock);
  3827. /* already mapped? */
  3828. if (em && em->start <= logical && em->start + em->len > logical) {
  3829. free_extent_map(em);
  3830. return 0;
  3831. } else if (em) {
  3832. free_extent_map(em);
  3833. }
  3834. em = alloc_extent_map();
  3835. if (!em)
  3836. return -ENOMEM;
  3837. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3838. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3839. if (!map) {
  3840. free_extent_map(em);
  3841. return -ENOMEM;
  3842. }
  3843. em->bdev = (struct block_device *)map;
  3844. em->start = logical;
  3845. em->len = length;
  3846. em->block_start = 0;
  3847. em->block_len = em->len;
  3848. map->num_stripes = num_stripes;
  3849. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3850. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3851. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3852. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3853. map->type = btrfs_chunk_type(leaf, chunk);
  3854. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3855. for (i = 0; i < num_stripes; i++) {
  3856. map->stripes[i].physical =
  3857. btrfs_stripe_offset_nr(leaf, chunk, i);
  3858. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3859. read_extent_buffer(leaf, uuid, (unsigned long)
  3860. btrfs_stripe_dev_uuid_nr(chunk, i),
  3861. BTRFS_UUID_SIZE);
  3862. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3863. NULL);
  3864. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3865. kfree(map);
  3866. free_extent_map(em);
  3867. return -EIO;
  3868. }
  3869. if (!map->stripes[i].dev) {
  3870. map->stripes[i].dev =
  3871. add_missing_dev(root, devid, uuid);
  3872. if (!map->stripes[i].dev) {
  3873. kfree(map);
  3874. free_extent_map(em);
  3875. return -EIO;
  3876. }
  3877. }
  3878. map->stripes[i].dev->in_fs_metadata = 1;
  3879. }
  3880. write_lock(&map_tree->map_tree.lock);
  3881. ret = add_extent_mapping(&map_tree->map_tree, em);
  3882. write_unlock(&map_tree->map_tree.lock);
  3883. BUG_ON(ret); /* Tree corruption */
  3884. free_extent_map(em);
  3885. return 0;
  3886. }
  3887. static void fill_device_from_item(struct extent_buffer *leaf,
  3888. struct btrfs_dev_item *dev_item,
  3889. struct btrfs_device *device)
  3890. {
  3891. unsigned long ptr;
  3892. device->devid = btrfs_device_id(leaf, dev_item);
  3893. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3894. device->total_bytes = device->disk_total_bytes;
  3895. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3896. device->type = btrfs_device_type(leaf, dev_item);
  3897. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3898. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3899. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3900. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3901. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3902. }
  3903. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3904. {
  3905. struct btrfs_fs_devices *fs_devices;
  3906. int ret;
  3907. BUG_ON(!mutex_is_locked(&uuid_mutex));
  3908. fs_devices = root->fs_info->fs_devices->seed;
  3909. while (fs_devices) {
  3910. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3911. ret = 0;
  3912. goto out;
  3913. }
  3914. fs_devices = fs_devices->seed;
  3915. }
  3916. fs_devices = find_fsid(fsid);
  3917. if (!fs_devices) {
  3918. ret = -ENOENT;
  3919. goto out;
  3920. }
  3921. fs_devices = clone_fs_devices(fs_devices);
  3922. if (IS_ERR(fs_devices)) {
  3923. ret = PTR_ERR(fs_devices);
  3924. goto out;
  3925. }
  3926. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3927. root->fs_info->bdev_holder);
  3928. if (ret) {
  3929. free_fs_devices(fs_devices);
  3930. goto out;
  3931. }
  3932. if (!fs_devices->seeding) {
  3933. __btrfs_close_devices(fs_devices);
  3934. free_fs_devices(fs_devices);
  3935. ret = -EINVAL;
  3936. goto out;
  3937. }
  3938. fs_devices->seed = root->fs_info->fs_devices->seed;
  3939. root->fs_info->fs_devices->seed = fs_devices;
  3940. out:
  3941. return ret;
  3942. }
  3943. static int read_one_dev(struct btrfs_root *root,
  3944. struct extent_buffer *leaf,
  3945. struct btrfs_dev_item *dev_item)
  3946. {
  3947. struct btrfs_device *device;
  3948. u64 devid;
  3949. int ret;
  3950. u8 fs_uuid[BTRFS_UUID_SIZE];
  3951. u8 dev_uuid[BTRFS_UUID_SIZE];
  3952. devid = btrfs_device_id(leaf, dev_item);
  3953. read_extent_buffer(leaf, dev_uuid,
  3954. (unsigned long)btrfs_device_uuid(dev_item),
  3955. BTRFS_UUID_SIZE);
  3956. read_extent_buffer(leaf, fs_uuid,
  3957. (unsigned long)btrfs_device_fsid(dev_item),
  3958. BTRFS_UUID_SIZE);
  3959. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3960. ret = open_seed_devices(root, fs_uuid);
  3961. if (ret && !btrfs_test_opt(root, DEGRADED))
  3962. return ret;
  3963. }
  3964. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3965. if (!device || !device->bdev) {
  3966. if (!btrfs_test_opt(root, DEGRADED))
  3967. return -EIO;
  3968. if (!device) {
  3969. printk(KERN_WARNING "warning devid %llu missing\n",
  3970. (unsigned long long)devid);
  3971. device = add_missing_dev(root, devid, dev_uuid);
  3972. if (!device)
  3973. return -ENOMEM;
  3974. } else if (!device->missing) {
  3975. /*
  3976. * this happens when a device that was properly setup
  3977. * in the device info lists suddenly goes bad.
  3978. * device->bdev is NULL, and so we have to set
  3979. * device->missing to one here
  3980. */
  3981. root->fs_info->fs_devices->missing_devices++;
  3982. device->missing = 1;
  3983. }
  3984. }
  3985. if (device->fs_devices != root->fs_info->fs_devices) {
  3986. BUG_ON(device->writeable);
  3987. if (device->generation !=
  3988. btrfs_device_generation(leaf, dev_item))
  3989. return -EINVAL;
  3990. }
  3991. fill_device_from_item(leaf, dev_item, device);
  3992. device->dev_root = root->fs_info->dev_root;
  3993. device->in_fs_metadata = 1;
  3994. if (device->writeable) {
  3995. device->fs_devices->total_rw_bytes += device->total_bytes;
  3996. spin_lock(&root->fs_info->free_chunk_lock);
  3997. root->fs_info->free_chunk_space += device->total_bytes -
  3998. device->bytes_used;
  3999. spin_unlock(&root->fs_info->free_chunk_lock);
  4000. }
  4001. ret = 0;
  4002. return ret;
  4003. }
  4004. int btrfs_read_sys_array(struct btrfs_root *root)
  4005. {
  4006. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  4007. struct extent_buffer *sb;
  4008. struct btrfs_disk_key *disk_key;
  4009. struct btrfs_chunk *chunk;
  4010. u8 *ptr;
  4011. unsigned long sb_ptr;
  4012. int ret = 0;
  4013. u32 num_stripes;
  4014. u32 array_size;
  4015. u32 len = 0;
  4016. u32 cur;
  4017. struct btrfs_key key;
  4018. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  4019. BTRFS_SUPER_INFO_SIZE);
  4020. if (!sb)
  4021. return -ENOMEM;
  4022. btrfs_set_buffer_uptodate(sb);
  4023. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  4024. /*
  4025. * The sb extent buffer is artifical and just used to read the system array.
  4026. * btrfs_set_buffer_uptodate() call does not properly mark all it's
  4027. * pages up-to-date when the page is larger: extent does not cover the
  4028. * whole page and consequently check_page_uptodate does not find all
  4029. * the page's extents up-to-date (the hole beyond sb),
  4030. * write_extent_buffer then triggers a WARN_ON.
  4031. *
  4032. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  4033. * but sb spans only this function. Add an explicit SetPageUptodate call
  4034. * to silence the warning eg. on PowerPC 64.
  4035. */
  4036. if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
  4037. SetPageUptodate(sb->pages[0]);
  4038. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  4039. array_size = btrfs_super_sys_array_size(super_copy);
  4040. ptr = super_copy->sys_chunk_array;
  4041. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  4042. cur = 0;
  4043. while (cur < array_size) {
  4044. disk_key = (struct btrfs_disk_key *)ptr;
  4045. btrfs_disk_key_to_cpu(&key, disk_key);
  4046. len = sizeof(*disk_key); ptr += len;
  4047. sb_ptr += len;
  4048. cur += len;
  4049. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  4050. chunk = (struct btrfs_chunk *)sb_ptr;
  4051. ret = read_one_chunk(root, &key, sb, chunk);
  4052. if (ret)
  4053. break;
  4054. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  4055. len = btrfs_chunk_item_size(num_stripes);
  4056. } else {
  4057. ret = -EIO;
  4058. break;
  4059. }
  4060. ptr += len;
  4061. sb_ptr += len;
  4062. cur += len;
  4063. }
  4064. free_extent_buffer(sb);
  4065. return ret;
  4066. }
  4067. int btrfs_read_chunk_tree(struct btrfs_root *root)
  4068. {
  4069. struct btrfs_path *path;
  4070. struct extent_buffer *leaf;
  4071. struct btrfs_key key;
  4072. struct btrfs_key found_key;
  4073. int ret;
  4074. int slot;
  4075. root = root->fs_info->chunk_root;
  4076. path = btrfs_alloc_path();
  4077. if (!path)
  4078. return -ENOMEM;
  4079. mutex_lock(&uuid_mutex);
  4080. lock_chunks(root);
  4081. /* first we search for all of the device items, and then we
  4082. * read in all of the chunk items. This way we can create chunk
  4083. * mappings that reference all of the devices that are afound
  4084. */
  4085. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  4086. key.offset = 0;
  4087. key.type = 0;
  4088. again:
  4089. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4090. if (ret < 0)
  4091. goto error;
  4092. while (1) {
  4093. leaf = path->nodes[0];
  4094. slot = path->slots[0];
  4095. if (slot >= btrfs_header_nritems(leaf)) {
  4096. ret = btrfs_next_leaf(root, path);
  4097. if (ret == 0)
  4098. continue;
  4099. if (ret < 0)
  4100. goto error;
  4101. break;
  4102. }
  4103. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4104. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4105. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  4106. break;
  4107. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  4108. struct btrfs_dev_item *dev_item;
  4109. dev_item = btrfs_item_ptr(leaf, slot,
  4110. struct btrfs_dev_item);
  4111. ret = read_one_dev(root, leaf, dev_item);
  4112. if (ret)
  4113. goto error;
  4114. }
  4115. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  4116. struct btrfs_chunk *chunk;
  4117. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  4118. ret = read_one_chunk(root, &found_key, leaf, chunk);
  4119. if (ret)
  4120. goto error;
  4121. }
  4122. path->slots[0]++;
  4123. }
  4124. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4125. key.objectid = 0;
  4126. btrfs_release_path(path);
  4127. goto again;
  4128. }
  4129. ret = 0;
  4130. error:
  4131. unlock_chunks(root);
  4132. mutex_unlock(&uuid_mutex);
  4133. btrfs_free_path(path);
  4134. return ret;
  4135. }
  4136. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  4137. {
  4138. int i;
  4139. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4140. btrfs_dev_stat_reset(dev, i);
  4141. }
  4142. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  4143. {
  4144. struct btrfs_key key;
  4145. struct btrfs_key found_key;
  4146. struct btrfs_root *dev_root = fs_info->dev_root;
  4147. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  4148. struct extent_buffer *eb;
  4149. int slot;
  4150. int ret = 0;
  4151. struct btrfs_device *device;
  4152. struct btrfs_path *path = NULL;
  4153. int i;
  4154. path = btrfs_alloc_path();
  4155. if (!path) {
  4156. ret = -ENOMEM;
  4157. goto out;
  4158. }
  4159. mutex_lock(&fs_devices->device_list_mutex);
  4160. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  4161. int item_size;
  4162. struct btrfs_dev_stats_item *ptr;
  4163. key.objectid = 0;
  4164. key.type = BTRFS_DEV_STATS_KEY;
  4165. key.offset = device->devid;
  4166. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  4167. if (ret) {
  4168. __btrfs_reset_dev_stats(device);
  4169. device->dev_stats_valid = 1;
  4170. btrfs_release_path(path);
  4171. continue;
  4172. }
  4173. slot = path->slots[0];
  4174. eb = path->nodes[0];
  4175. btrfs_item_key_to_cpu(eb, &found_key, slot);
  4176. item_size = btrfs_item_size_nr(eb, slot);
  4177. ptr = btrfs_item_ptr(eb, slot,
  4178. struct btrfs_dev_stats_item);
  4179. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  4180. if (item_size >= (1 + i) * sizeof(__le64))
  4181. btrfs_dev_stat_set(device, i,
  4182. btrfs_dev_stats_value(eb, ptr, i));
  4183. else
  4184. btrfs_dev_stat_reset(device, i);
  4185. }
  4186. device->dev_stats_valid = 1;
  4187. btrfs_dev_stat_print_on_load(device);
  4188. btrfs_release_path(path);
  4189. }
  4190. mutex_unlock(&fs_devices->device_list_mutex);
  4191. out:
  4192. btrfs_free_path(path);
  4193. return ret < 0 ? ret : 0;
  4194. }
  4195. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  4196. struct btrfs_root *dev_root,
  4197. struct btrfs_device *device)
  4198. {
  4199. struct btrfs_path *path;
  4200. struct btrfs_key key;
  4201. struct extent_buffer *eb;
  4202. struct btrfs_dev_stats_item *ptr;
  4203. int ret;
  4204. int i;
  4205. key.objectid = 0;
  4206. key.type = BTRFS_DEV_STATS_KEY;
  4207. key.offset = device->devid;
  4208. path = btrfs_alloc_path();
  4209. BUG_ON(!path);
  4210. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  4211. if (ret < 0) {
  4212. printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
  4213. ret, rcu_str_deref(device->name));
  4214. goto out;
  4215. }
  4216. if (ret == 0 &&
  4217. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  4218. /* need to delete old one and insert a new one */
  4219. ret = btrfs_del_item(trans, dev_root, path);
  4220. if (ret != 0) {
  4221. printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
  4222. rcu_str_deref(device->name), ret);
  4223. goto out;
  4224. }
  4225. ret = 1;
  4226. }
  4227. if (ret == 1) {
  4228. /* need to insert a new item */
  4229. btrfs_release_path(path);
  4230. ret = btrfs_insert_empty_item(trans, dev_root, path,
  4231. &key, sizeof(*ptr));
  4232. if (ret < 0) {
  4233. printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
  4234. rcu_str_deref(device->name), ret);
  4235. goto out;
  4236. }
  4237. }
  4238. eb = path->nodes[0];
  4239. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  4240. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4241. btrfs_set_dev_stats_value(eb, ptr, i,
  4242. btrfs_dev_stat_read(device, i));
  4243. btrfs_mark_buffer_dirty(eb);
  4244. out:
  4245. btrfs_free_path(path);
  4246. return ret;
  4247. }
  4248. /*
  4249. * called from commit_transaction. Writes all changed device stats to disk.
  4250. */
  4251. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  4252. struct btrfs_fs_info *fs_info)
  4253. {
  4254. struct btrfs_root *dev_root = fs_info->dev_root;
  4255. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  4256. struct btrfs_device *device;
  4257. int ret = 0;
  4258. mutex_lock(&fs_devices->device_list_mutex);
  4259. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  4260. if (!device->dev_stats_valid || !device->dev_stats_dirty)
  4261. continue;
  4262. ret = update_dev_stat_item(trans, dev_root, device);
  4263. if (!ret)
  4264. device->dev_stats_dirty = 0;
  4265. }
  4266. mutex_unlock(&fs_devices->device_list_mutex);
  4267. return ret;
  4268. }
  4269. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  4270. {
  4271. btrfs_dev_stat_inc(dev, index);
  4272. btrfs_dev_stat_print_on_error(dev);
  4273. }
  4274. void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  4275. {
  4276. if (!dev->dev_stats_valid)
  4277. return;
  4278. printk_ratelimited_in_rcu(KERN_ERR
  4279. "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  4280. rcu_str_deref(dev->name),
  4281. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  4282. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  4283. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  4284. btrfs_dev_stat_read(dev,
  4285. BTRFS_DEV_STAT_CORRUPTION_ERRS),
  4286. btrfs_dev_stat_read(dev,
  4287. BTRFS_DEV_STAT_GENERATION_ERRS));
  4288. }
  4289. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  4290. {
  4291. int i;
  4292. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4293. if (btrfs_dev_stat_read(dev, i) != 0)
  4294. break;
  4295. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  4296. return; /* all values == 0, suppress message */
  4297. printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  4298. rcu_str_deref(dev->name),
  4299. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  4300. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  4301. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  4302. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  4303. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  4304. }
  4305. int btrfs_get_dev_stats(struct btrfs_root *root,
  4306. struct btrfs_ioctl_get_dev_stats *stats)
  4307. {
  4308. struct btrfs_device *dev;
  4309. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  4310. int i;
  4311. mutex_lock(&fs_devices->device_list_mutex);
  4312. dev = btrfs_find_device(root, stats->devid, NULL, NULL);
  4313. mutex_unlock(&fs_devices->device_list_mutex);
  4314. if (!dev) {
  4315. printk(KERN_WARNING
  4316. "btrfs: get dev_stats failed, device not found\n");
  4317. return -ENODEV;
  4318. } else if (!dev->dev_stats_valid) {
  4319. printk(KERN_WARNING
  4320. "btrfs: get dev_stats failed, not yet valid\n");
  4321. return -ENODEV;
  4322. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  4323. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  4324. if (stats->nr_items > i)
  4325. stats->values[i] =
  4326. btrfs_dev_stat_read_and_reset(dev, i);
  4327. else
  4328. btrfs_dev_stat_reset(dev, i);
  4329. }
  4330. } else {
  4331. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4332. if (stats->nr_items > i)
  4333. stats->values[i] = btrfs_dev_stat_read(dev, i);
  4334. }
  4335. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  4336. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  4337. return 0;
  4338. }