volumes.c 135 KB

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