volumes.c 143 KB

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