volumes.c 129 KB

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