extent_io.c 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053
  1. #include <linux/bitops.h>
  2. #include <linux/slab.h>
  3. #include <linux/bio.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/page-flags.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/swap.h>
  10. #include <linux/writeback.h>
  11. #include <linux/pagevec.h>
  12. #include <linux/prefetch.h>
  13. #include <linux/cleancache.h>
  14. #include "extent_io.h"
  15. #include "extent_map.h"
  16. #include "ctree.h"
  17. #include "btrfs_inode.h"
  18. #include "volumes.h"
  19. #include "check-integrity.h"
  20. #include "locking.h"
  21. #include "rcu-string.h"
  22. #include "backref.h"
  23. #include "transaction.h"
  24. static struct kmem_cache *extent_state_cache;
  25. static struct kmem_cache *extent_buffer_cache;
  26. static struct bio_set *btrfs_bioset;
  27. static inline bool extent_state_in_tree(const struct extent_state *state)
  28. {
  29. return !RB_EMPTY_NODE(&state->rb_node);
  30. }
  31. #ifdef CONFIG_BTRFS_DEBUG
  32. static LIST_HEAD(buffers);
  33. static LIST_HEAD(states);
  34. static DEFINE_SPINLOCK(leak_lock);
  35. static inline
  36. void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
  37. {
  38. unsigned long flags;
  39. spin_lock_irqsave(&leak_lock, flags);
  40. list_add(new, head);
  41. spin_unlock_irqrestore(&leak_lock, flags);
  42. }
  43. static inline
  44. void btrfs_leak_debug_del(struct list_head *entry)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&leak_lock, flags);
  48. list_del(entry);
  49. spin_unlock_irqrestore(&leak_lock, flags);
  50. }
  51. static inline
  52. void btrfs_leak_debug_check(void)
  53. {
  54. struct extent_state *state;
  55. struct extent_buffer *eb;
  56. while (!list_empty(&states)) {
  57. state = list_entry(states.next, struct extent_state, leak_list);
  58. pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
  59. state->start, state->end, state->state,
  60. extent_state_in_tree(state),
  61. refcount_read(&state->refs));
  62. list_del(&state->leak_list);
  63. kmem_cache_free(extent_state_cache, state);
  64. }
  65. while (!list_empty(&buffers)) {
  66. eb = list_entry(buffers.next, struct extent_buffer, leak_list);
  67. pr_err("BTRFS: buffer leak start %llu len %lu refs %d\n",
  68. eb->start, eb->len, atomic_read(&eb->refs));
  69. list_del(&eb->leak_list);
  70. kmem_cache_free(extent_buffer_cache, eb);
  71. }
  72. }
  73. #define btrfs_debug_check_extent_io_range(tree, start, end) \
  74. __btrfs_debug_check_extent_io_range(__func__, (tree), (start), (end))
  75. static inline void __btrfs_debug_check_extent_io_range(const char *caller,
  76. struct extent_io_tree *tree, u64 start, u64 end)
  77. {
  78. struct inode *inode;
  79. u64 isize;
  80. if (!tree->mapping)
  81. return;
  82. inode = tree->mapping->host;
  83. isize = i_size_read(inode);
  84. if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
  85. btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
  86. "%s: ino %llu isize %llu odd range [%llu,%llu]",
  87. caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
  88. }
  89. }
  90. #else
  91. #define btrfs_leak_debug_add(new, head) do {} while (0)
  92. #define btrfs_leak_debug_del(entry) do {} while (0)
  93. #define btrfs_leak_debug_check() do {} while (0)
  94. #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
  95. #endif
  96. #define BUFFER_LRU_MAX 64
  97. struct tree_entry {
  98. u64 start;
  99. u64 end;
  100. struct rb_node rb_node;
  101. };
  102. struct extent_page_data {
  103. struct bio *bio;
  104. struct extent_io_tree *tree;
  105. get_extent_t *get_extent;
  106. unsigned long bio_flags;
  107. /* tells writepage not to lock the state bits for this range
  108. * it still does the unlocking
  109. */
  110. unsigned int extent_locked:1;
  111. /* tells the submit_bio code to use REQ_SYNC */
  112. unsigned int sync_io:1;
  113. };
  114. static void add_extent_changeset(struct extent_state *state, unsigned bits,
  115. struct extent_changeset *changeset,
  116. int set)
  117. {
  118. int ret;
  119. if (!changeset)
  120. return;
  121. if (set && (state->state & bits) == bits)
  122. return;
  123. if (!set && (state->state & bits) == 0)
  124. return;
  125. changeset->bytes_changed += state->end - state->start + 1;
  126. ret = ulist_add(&changeset->range_changed, state->start, state->end,
  127. GFP_ATOMIC);
  128. /* ENOMEM */
  129. BUG_ON(ret < 0);
  130. }
  131. static noinline void flush_write_bio(void *data);
  132. static inline struct btrfs_fs_info *
  133. tree_fs_info(struct extent_io_tree *tree)
  134. {
  135. if (!tree->mapping)
  136. return NULL;
  137. return btrfs_sb(tree->mapping->host->i_sb);
  138. }
  139. int __init extent_io_init(void)
  140. {
  141. extent_state_cache = kmem_cache_create("btrfs_extent_state",
  142. sizeof(struct extent_state), 0,
  143. SLAB_MEM_SPREAD, NULL);
  144. if (!extent_state_cache)
  145. return -ENOMEM;
  146. extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
  147. sizeof(struct extent_buffer), 0,
  148. SLAB_MEM_SPREAD, NULL);
  149. if (!extent_buffer_cache)
  150. goto free_state_cache;
  151. btrfs_bioset = bioset_create(BIO_POOL_SIZE,
  152. offsetof(struct btrfs_io_bio, bio),
  153. BIOSET_NEED_BVECS);
  154. if (!btrfs_bioset)
  155. goto free_buffer_cache;
  156. if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
  157. goto free_bioset;
  158. return 0;
  159. free_bioset:
  160. bioset_free(btrfs_bioset);
  161. btrfs_bioset = NULL;
  162. free_buffer_cache:
  163. kmem_cache_destroy(extent_buffer_cache);
  164. extent_buffer_cache = NULL;
  165. free_state_cache:
  166. kmem_cache_destroy(extent_state_cache);
  167. extent_state_cache = NULL;
  168. return -ENOMEM;
  169. }
  170. void extent_io_exit(void)
  171. {
  172. btrfs_leak_debug_check();
  173. /*
  174. * Make sure all delayed rcu free are flushed before we
  175. * destroy caches.
  176. */
  177. rcu_barrier();
  178. kmem_cache_destroy(extent_state_cache);
  179. kmem_cache_destroy(extent_buffer_cache);
  180. if (btrfs_bioset)
  181. bioset_free(btrfs_bioset);
  182. }
  183. void extent_io_tree_init(struct extent_io_tree *tree,
  184. struct address_space *mapping)
  185. {
  186. tree->state = RB_ROOT;
  187. tree->ops = NULL;
  188. tree->dirty_bytes = 0;
  189. spin_lock_init(&tree->lock);
  190. tree->mapping = mapping;
  191. }
  192. static struct extent_state *alloc_extent_state(gfp_t mask)
  193. {
  194. struct extent_state *state;
  195. /*
  196. * The given mask might be not appropriate for the slab allocator,
  197. * drop the unsupported bits
  198. */
  199. mask &= ~(__GFP_DMA32|__GFP_HIGHMEM);
  200. state = kmem_cache_alloc(extent_state_cache, mask);
  201. if (!state)
  202. return state;
  203. state->state = 0;
  204. state->failrec = NULL;
  205. RB_CLEAR_NODE(&state->rb_node);
  206. btrfs_leak_debug_add(&state->leak_list, &states);
  207. refcount_set(&state->refs, 1);
  208. init_waitqueue_head(&state->wq);
  209. trace_alloc_extent_state(state, mask, _RET_IP_);
  210. return state;
  211. }
  212. void free_extent_state(struct extent_state *state)
  213. {
  214. if (!state)
  215. return;
  216. if (refcount_dec_and_test(&state->refs)) {
  217. WARN_ON(extent_state_in_tree(state));
  218. btrfs_leak_debug_del(&state->leak_list);
  219. trace_free_extent_state(state, _RET_IP_);
  220. kmem_cache_free(extent_state_cache, state);
  221. }
  222. }
  223. static struct rb_node *tree_insert(struct rb_root *root,
  224. struct rb_node *search_start,
  225. u64 offset,
  226. struct rb_node *node,
  227. struct rb_node ***p_in,
  228. struct rb_node **parent_in)
  229. {
  230. struct rb_node **p;
  231. struct rb_node *parent = NULL;
  232. struct tree_entry *entry;
  233. if (p_in && parent_in) {
  234. p = *p_in;
  235. parent = *parent_in;
  236. goto do_insert;
  237. }
  238. p = search_start ? &search_start : &root->rb_node;
  239. while (*p) {
  240. parent = *p;
  241. entry = rb_entry(parent, struct tree_entry, rb_node);
  242. if (offset < entry->start)
  243. p = &(*p)->rb_left;
  244. else if (offset > entry->end)
  245. p = &(*p)->rb_right;
  246. else
  247. return parent;
  248. }
  249. do_insert:
  250. rb_link_node(node, parent, p);
  251. rb_insert_color(node, root);
  252. return NULL;
  253. }
  254. static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
  255. struct rb_node **prev_ret,
  256. struct rb_node **next_ret,
  257. struct rb_node ***p_ret,
  258. struct rb_node **parent_ret)
  259. {
  260. struct rb_root *root = &tree->state;
  261. struct rb_node **n = &root->rb_node;
  262. struct rb_node *prev = NULL;
  263. struct rb_node *orig_prev = NULL;
  264. struct tree_entry *entry;
  265. struct tree_entry *prev_entry = NULL;
  266. while (*n) {
  267. prev = *n;
  268. entry = rb_entry(prev, struct tree_entry, rb_node);
  269. prev_entry = entry;
  270. if (offset < entry->start)
  271. n = &(*n)->rb_left;
  272. else if (offset > entry->end)
  273. n = &(*n)->rb_right;
  274. else
  275. return *n;
  276. }
  277. if (p_ret)
  278. *p_ret = n;
  279. if (parent_ret)
  280. *parent_ret = prev;
  281. if (prev_ret) {
  282. orig_prev = prev;
  283. while (prev && offset > prev_entry->end) {
  284. prev = rb_next(prev);
  285. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  286. }
  287. *prev_ret = prev;
  288. prev = orig_prev;
  289. }
  290. if (next_ret) {
  291. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  292. while (prev && offset < prev_entry->start) {
  293. prev = rb_prev(prev);
  294. prev_entry = rb_entry(prev, struct tree_entry, rb_node);
  295. }
  296. *next_ret = prev;
  297. }
  298. return NULL;
  299. }
  300. static inline struct rb_node *
  301. tree_search_for_insert(struct extent_io_tree *tree,
  302. u64 offset,
  303. struct rb_node ***p_ret,
  304. struct rb_node **parent_ret)
  305. {
  306. struct rb_node *prev = NULL;
  307. struct rb_node *ret;
  308. ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
  309. if (!ret)
  310. return prev;
  311. return ret;
  312. }
  313. static inline struct rb_node *tree_search(struct extent_io_tree *tree,
  314. u64 offset)
  315. {
  316. return tree_search_for_insert(tree, offset, NULL, NULL);
  317. }
  318. static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
  319. struct extent_state *other)
  320. {
  321. if (tree->ops && tree->ops->merge_extent_hook)
  322. tree->ops->merge_extent_hook(tree->mapping->host, new,
  323. other);
  324. }
  325. /*
  326. * utility function to look for merge candidates inside a given range.
  327. * Any extents with matching state are merged together into a single
  328. * extent in the tree. Extents with EXTENT_IO in their state field
  329. * are not merged because the end_io handlers need to be able to do
  330. * operations on them without sleeping (or doing allocations/splits).
  331. *
  332. * This should be called with the tree lock held.
  333. */
  334. static void merge_state(struct extent_io_tree *tree,
  335. struct extent_state *state)
  336. {
  337. struct extent_state *other;
  338. struct rb_node *other_node;
  339. if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  340. return;
  341. other_node = rb_prev(&state->rb_node);
  342. if (other_node) {
  343. other = rb_entry(other_node, struct extent_state, rb_node);
  344. if (other->end == state->start - 1 &&
  345. other->state == state->state) {
  346. merge_cb(tree, state, other);
  347. state->start = other->start;
  348. rb_erase(&other->rb_node, &tree->state);
  349. RB_CLEAR_NODE(&other->rb_node);
  350. free_extent_state(other);
  351. }
  352. }
  353. other_node = rb_next(&state->rb_node);
  354. if (other_node) {
  355. other = rb_entry(other_node, struct extent_state, rb_node);
  356. if (other->start == state->end + 1 &&
  357. other->state == state->state) {
  358. merge_cb(tree, state, other);
  359. state->end = other->end;
  360. rb_erase(&other->rb_node, &tree->state);
  361. RB_CLEAR_NODE(&other->rb_node);
  362. free_extent_state(other);
  363. }
  364. }
  365. }
  366. static void set_state_cb(struct extent_io_tree *tree,
  367. struct extent_state *state, unsigned *bits)
  368. {
  369. if (tree->ops && tree->ops->set_bit_hook)
  370. tree->ops->set_bit_hook(tree->mapping->host, state, bits);
  371. }
  372. static void clear_state_cb(struct extent_io_tree *tree,
  373. struct extent_state *state, unsigned *bits)
  374. {
  375. if (tree->ops && tree->ops->clear_bit_hook)
  376. tree->ops->clear_bit_hook(BTRFS_I(tree->mapping->host),
  377. state, bits);
  378. }
  379. static void set_state_bits(struct extent_io_tree *tree,
  380. struct extent_state *state, unsigned *bits,
  381. struct extent_changeset *changeset);
  382. /*
  383. * insert an extent_state struct into the tree. 'bits' are set on the
  384. * struct before it is inserted.
  385. *
  386. * This may return -EEXIST if the extent is already there, in which case the
  387. * state struct is freed.
  388. *
  389. * The tree lock is not taken internally. This is a utility function and
  390. * probably isn't what you want to call (see set/clear_extent_bit).
  391. */
  392. static int insert_state(struct extent_io_tree *tree,
  393. struct extent_state *state, u64 start, u64 end,
  394. struct rb_node ***p,
  395. struct rb_node **parent,
  396. unsigned *bits, struct extent_changeset *changeset)
  397. {
  398. struct rb_node *node;
  399. if (end < start)
  400. WARN(1, KERN_ERR "BTRFS: end < start %llu %llu\n",
  401. end, start);
  402. state->start = start;
  403. state->end = end;
  404. set_state_bits(tree, state, bits, changeset);
  405. node = tree_insert(&tree->state, NULL, end, &state->rb_node, p, parent);
  406. if (node) {
  407. struct extent_state *found;
  408. found = rb_entry(node, struct extent_state, rb_node);
  409. pr_err("BTRFS: found node %llu %llu on insert of %llu %llu\n",
  410. found->start, found->end, start, end);
  411. return -EEXIST;
  412. }
  413. merge_state(tree, state);
  414. return 0;
  415. }
  416. static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
  417. u64 split)
  418. {
  419. if (tree->ops && tree->ops->split_extent_hook)
  420. tree->ops->split_extent_hook(tree->mapping->host, orig, split);
  421. }
  422. /*
  423. * split a given extent state struct in two, inserting the preallocated
  424. * struct 'prealloc' as the newly created second half. 'split' indicates an
  425. * offset inside 'orig' where it should be split.
  426. *
  427. * Before calling,
  428. * the tree has 'orig' at [orig->start, orig->end]. After calling, there
  429. * are two extent state structs in the tree:
  430. * prealloc: [orig->start, split - 1]
  431. * orig: [ split, orig->end ]
  432. *
  433. * The tree locks are not taken by this function. They need to be held
  434. * by the caller.
  435. */
  436. static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
  437. struct extent_state *prealloc, u64 split)
  438. {
  439. struct rb_node *node;
  440. split_cb(tree, orig, split);
  441. prealloc->start = orig->start;
  442. prealloc->end = split - 1;
  443. prealloc->state = orig->state;
  444. orig->start = split;
  445. node = tree_insert(&tree->state, &orig->rb_node, prealloc->end,
  446. &prealloc->rb_node, NULL, NULL);
  447. if (node) {
  448. free_extent_state(prealloc);
  449. return -EEXIST;
  450. }
  451. return 0;
  452. }
  453. static struct extent_state *next_state(struct extent_state *state)
  454. {
  455. struct rb_node *next = rb_next(&state->rb_node);
  456. if (next)
  457. return rb_entry(next, struct extent_state, rb_node);
  458. else
  459. return NULL;
  460. }
  461. /*
  462. * utility function to clear some bits in an extent state struct.
  463. * it will optionally wake up any one waiting on this state (wake == 1).
  464. *
  465. * If no bits are set on the state struct after clearing things, the
  466. * struct is freed and removed from the tree
  467. */
  468. static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
  469. struct extent_state *state,
  470. unsigned *bits, int wake,
  471. struct extent_changeset *changeset)
  472. {
  473. struct extent_state *next;
  474. unsigned bits_to_clear = *bits & ~EXTENT_CTLBITS;
  475. if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
  476. u64 range = state->end - state->start + 1;
  477. WARN_ON(range > tree->dirty_bytes);
  478. tree->dirty_bytes -= range;
  479. }
  480. clear_state_cb(tree, state, bits);
  481. add_extent_changeset(state, bits_to_clear, changeset, 0);
  482. state->state &= ~bits_to_clear;
  483. if (wake)
  484. wake_up(&state->wq);
  485. if (state->state == 0) {
  486. next = next_state(state);
  487. if (extent_state_in_tree(state)) {
  488. rb_erase(&state->rb_node, &tree->state);
  489. RB_CLEAR_NODE(&state->rb_node);
  490. free_extent_state(state);
  491. } else {
  492. WARN_ON(1);
  493. }
  494. } else {
  495. merge_state(tree, state);
  496. next = next_state(state);
  497. }
  498. return next;
  499. }
  500. static struct extent_state *
  501. alloc_extent_state_atomic(struct extent_state *prealloc)
  502. {
  503. if (!prealloc)
  504. prealloc = alloc_extent_state(GFP_ATOMIC);
  505. return prealloc;
  506. }
  507. static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
  508. {
  509. btrfs_panic(tree_fs_info(tree), err,
  510. "Locking error: Extent tree was modified by another thread while locked.");
  511. }
  512. /*
  513. * clear some bits on a range in the tree. This may require splitting
  514. * or inserting elements in the tree, so the gfp mask is used to
  515. * indicate which allocations or sleeping are allowed.
  516. *
  517. * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
  518. * the given range from the tree regardless of state (ie for truncate).
  519. *
  520. * the range [start, end] is inclusive.
  521. *
  522. * This takes the tree lock, and returns 0 on success and < 0 on error.
  523. */
  524. static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  525. unsigned bits, int wake, int delete,
  526. struct extent_state **cached_state,
  527. gfp_t mask, struct extent_changeset *changeset)
  528. {
  529. struct extent_state *state;
  530. struct extent_state *cached;
  531. struct extent_state *prealloc = NULL;
  532. struct rb_node *node;
  533. u64 last_end;
  534. int err;
  535. int clear = 0;
  536. btrfs_debug_check_extent_io_range(tree, start, end);
  537. if (bits & EXTENT_DELALLOC)
  538. bits |= EXTENT_NORESERVE;
  539. if (delete)
  540. bits |= ~EXTENT_CTLBITS;
  541. bits |= EXTENT_FIRST_DELALLOC;
  542. if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
  543. clear = 1;
  544. again:
  545. if (!prealloc && gfpflags_allow_blocking(mask)) {
  546. /*
  547. * Don't care for allocation failure here because we might end
  548. * up not needing the pre-allocated extent state at all, which
  549. * is the case if we only have in the tree extent states that
  550. * cover our input range and don't cover too any other range.
  551. * If we end up needing a new extent state we allocate it later.
  552. */
  553. prealloc = alloc_extent_state(mask);
  554. }
  555. spin_lock(&tree->lock);
  556. if (cached_state) {
  557. cached = *cached_state;
  558. if (clear) {
  559. *cached_state = NULL;
  560. cached_state = NULL;
  561. }
  562. if (cached && extent_state_in_tree(cached) &&
  563. cached->start <= start && cached->end > start) {
  564. if (clear)
  565. refcount_dec(&cached->refs);
  566. state = cached;
  567. goto hit_next;
  568. }
  569. if (clear)
  570. free_extent_state(cached);
  571. }
  572. /*
  573. * this search will find the extents that end after
  574. * our range starts
  575. */
  576. node = tree_search(tree, start);
  577. if (!node)
  578. goto out;
  579. state = rb_entry(node, struct extent_state, rb_node);
  580. hit_next:
  581. if (state->start > end)
  582. goto out;
  583. WARN_ON(state->end < start);
  584. last_end = state->end;
  585. /* the state doesn't have the wanted bits, go ahead */
  586. if (!(state->state & bits)) {
  587. state = next_state(state);
  588. goto next;
  589. }
  590. /*
  591. * | ---- desired range ---- |
  592. * | state | or
  593. * | ------------- state -------------- |
  594. *
  595. * We need to split the extent we found, and may flip
  596. * bits on second half.
  597. *
  598. * If the extent we found extends past our range, we
  599. * just split and search again. It'll get split again
  600. * the next time though.
  601. *
  602. * If the extent we found is inside our range, we clear
  603. * the desired bit on it.
  604. */
  605. if (state->start < start) {
  606. prealloc = alloc_extent_state_atomic(prealloc);
  607. BUG_ON(!prealloc);
  608. err = split_state(tree, state, prealloc, start);
  609. if (err)
  610. extent_io_tree_panic(tree, err);
  611. prealloc = NULL;
  612. if (err)
  613. goto out;
  614. if (state->end <= end) {
  615. state = clear_state_bit(tree, state, &bits, wake,
  616. changeset);
  617. goto next;
  618. }
  619. goto search_again;
  620. }
  621. /*
  622. * | ---- desired range ---- |
  623. * | state |
  624. * We need to split the extent, and clear the bit
  625. * on the first half
  626. */
  627. if (state->start <= end && state->end > end) {
  628. prealloc = alloc_extent_state_atomic(prealloc);
  629. BUG_ON(!prealloc);
  630. err = split_state(tree, state, prealloc, end + 1);
  631. if (err)
  632. extent_io_tree_panic(tree, err);
  633. if (wake)
  634. wake_up(&state->wq);
  635. clear_state_bit(tree, prealloc, &bits, wake, changeset);
  636. prealloc = NULL;
  637. goto out;
  638. }
  639. state = clear_state_bit(tree, state, &bits, wake, changeset);
  640. next:
  641. if (last_end == (u64)-1)
  642. goto out;
  643. start = last_end + 1;
  644. if (start <= end && state && !need_resched())
  645. goto hit_next;
  646. search_again:
  647. if (start > end)
  648. goto out;
  649. spin_unlock(&tree->lock);
  650. if (gfpflags_allow_blocking(mask))
  651. cond_resched();
  652. goto again;
  653. out:
  654. spin_unlock(&tree->lock);
  655. if (prealloc)
  656. free_extent_state(prealloc);
  657. return 0;
  658. }
  659. static void wait_on_state(struct extent_io_tree *tree,
  660. struct extent_state *state)
  661. __releases(tree->lock)
  662. __acquires(tree->lock)
  663. {
  664. DEFINE_WAIT(wait);
  665. prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
  666. spin_unlock(&tree->lock);
  667. schedule();
  668. spin_lock(&tree->lock);
  669. finish_wait(&state->wq, &wait);
  670. }
  671. /*
  672. * waits for one or more bits to clear on a range in the state tree.
  673. * The range [start, end] is inclusive.
  674. * The tree lock is taken by this function
  675. */
  676. static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  677. unsigned long bits)
  678. {
  679. struct extent_state *state;
  680. struct rb_node *node;
  681. btrfs_debug_check_extent_io_range(tree, start, end);
  682. spin_lock(&tree->lock);
  683. again:
  684. while (1) {
  685. /*
  686. * this search will find all the extents that end after
  687. * our range starts
  688. */
  689. node = tree_search(tree, start);
  690. process_node:
  691. if (!node)
  692. break;
  693. state = rb_entry(node, struct extent_state, rb_node);
  694. if (state->start > end)
  695. goto out;
  696. if (state->state & bits) {
  697. start = state->start;
  698. refcount_inc(&state->refs);
  699. wait_on_state(tree, state);
  700. free_extent_state(state);
  701. goto again;
  702. }
  703. start = state->end + 1;
  704. if (start > end)
  705. break;
  706. if (!cond_resched_lock(&tree->lock)) {
  707. node = rb_next(node);
  708. goto process_node;
  709. }
  710. }
  711. out:
  712. spin_unlock(&tree->lock);
  713. }
  714. static void set_state_bits(struct extent_io_tree *tree,
  715. struct extent_state *state,
  716. unsigned *bits, struct extent_changeset *changeset)
  717. {
  718. unsigned bits_to_set = *bits & ~EXTENT_CTLBITS;
  719. set_state_cb(tree, state, bits);
  720. if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
  721. u64 range = state->end - state->start + 1;
  722. tree->dirty_bytes += range;
  723. }
  724. add_extent_changeset(state, bits_to_set, changeset, 1);
  725. state->state |= bits_to_set;
  726. }
  727. static void cache_state_if_flags(struct extent_state *state,
  728. struct extent_state **cached_ptr,
  729. unsigned flags)
  730. {
  731. if (cached_ptr && !(*cached_ptr)) {
  732. if (!flags || (state->state & flags)) {
  733. *cached_ptr = state;
  734. refcount_inc(&state->refs);
  735. }
  736. }
  737. }
  738. static void cache_state(struct extent_state *state,
  739. struct extent_state **cached_ptr)
  740. {
  741. return cache_state_if_flags(state, cached_ptr,
  742. EXTENT_IOBITS | EXTENT_BOUNDARY);
  743. }
  744. /*
  745. * set some bits on a range in the tree. This may require allocations or
  746. * sleeping, so the gfp mask is used to indicate what is allowed.
  747. *
  748. * If any of the exclusive bits are set, this will fail with -EEXIST if some
  749. * part of the range already has the desired bits set. The start of the
  750. * existing range is returned in failed_start in this case.
  751. *
  752. * [start, end] is inclusive This takes the tree lock.
  753. */
  754. static int __must_check
  755. __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  756. unsigned bits, unsigned exclusive_bits,
  757. u64 *failed_start, struct extent_state **cached_state,
  758. gfp_t mask, struct extent_changeset *changeset)
  759. {
  760. struct extent_state *state;
  761. struct extent_state *prealloc = NULL;
  762. struct rb_node *node;
  763. struct rb_node **p;
  764. struct rb_node *parent;
  765. int err = 0;
  766. u64 last_start;
  767. u64 last_end;
  768. btrfs_debug_check_extent_io_range(tree, start, end);
  769. bits |= EXTENT_FIRST_DELALLOC;
  770. again:
  771. if (!prealloc && gfpflags_allow_blocking(mask)) {
  772. /*
  773. * Don't care for allocation failure here because we might end
  774. * up not needing the pre-allocated extent state at all, which
  775. * is the case if we only have in the tree extent states that
  776. * cover our input range and don't cover too any other range.
  777. * If we end up needing a new extent state we allocate it later.
  778. */
  779. prealloc = alloc_extent_state(mask);
  780. }
  781. spin_lock(&tree->lock);
  782. if (cached_state && *cached_state) {
  783. state = *cached_state;
  784. if (state->start <= start && state->end > start &&
  785. extent_state_in_tree(state)) {
  786. node = &state->rb_node;
  787. goto hit_next;
  788. }
  789. }
  790. /*
  791. * this search will find all the extents that end after
  792. * our range starts.
  793. */
  794. node = tree_search_for_insert(tree, start, &p, &parent);
  795. if (!node) {
  796. prealloc = alloc_extent_state_atomic(prealloc);
  797. BUG_ON(!prealloc);
  798. err = insert_state(tree, prealloc, start, end,
  799. &p, &parent, &bits, changeset);
  800. if (err)
  801. extent_io_tree_panic(tree, err);
  802. cache_state(prealloc, cached_state);
  803. prealloc = NULL;
  804. goto out;
  805. }
  806. state = rb_entry(node, struct extent_state, rb_node);
  807. hit_next:
  808. last_start = state->start;
  809. last_end = state->end;
  810. /*
  811. * | ---- desired range ---- |
  812. * | state |
  813. *
  814. * Just lock what we found and keep going
  815. */
  816. if (state->start == start && state->end <= end) {
  817. if (state->state & exclusive_bits) {
  818. *failed_start = state->start;
  819. err = -EEXIST;
  820. goto out;
  821. }
  822. set_state_bits(tree, state, &bits, changeset);
  823. cache_state(state, cached_state);
  824. merge_state(tree, state);
  825. if (last_end == (u64)-1)
  826. goto out;
  827. start = last_end + 1;
  828. state = next_state(state);
  829. if (start < end && state && state->start == start &&
  830. !need_resched())
  831. goto hit_next;
  832. goto search_again;
  833. }
  834. /*
  835. * | ---- desired range ---- |
  836. * | state |
  837. * or
  838. * | ------------- state -------------- |
  839. *
  840. * We need to split the extent we found, and may flip bits on
  841. * second half.
  842. *
  843. * If the extent we found extends past our
  844. * range, we just split and search again. It'll get split
  845. * again the next time though.
  846. *
  847. * If the extent we found is inside our range, we set the
  848. * desired bit on it.
  849. */
  850. if (state->start < start) {
  851. if (state->state & exclusive_bits) {
  852. *failed_start = start;
  853. err = -EEXIST;
  854. goto out;
  855. }
  856. prealloc = alloc_extent_state_atomic(prealloc);
  857. BUG_ON(!prealloc);
  858. err = split_state(tree, state, prealloc, start);
  859. if (err)
  860. extent_io_tree_panic(tree, err);
  861. prealloc = NULL;
  862. if (err)
  863. goto out;
  864. if (state->end <= end) {
  865. set_state_bits(tree, state, &bits, changeset);
  866. cache_state(state, cached_state);
  867. merge_state(tree, state);
  868. if (last_end == (u64)-1)
  869. goto out;
  870. start = last_end + 1;
  871. state = next_state(state);
  872. if (start < end && state && state->start == start &&
  873. !need_resched())
  874. goto hit_next;
  875. }
  876. goto search_again;
  877. }
  878. /*
  879. * | ---- desired range ---- |
  880. * | state | or | state |
  881. *
  882. * There's a hole, we need to insert something in it and
  883. * ignore the extent we found.
  884. */
  885. if (state->start > start) {
  886. u64 this_end;
  887. if (end < last_start)
  888. this_end = end;
  889. else
  890. this_end = last_start - 1;
  891. prealloc = alloc_extent_state_atomic(prealloc);
  892. BUG_ON(!prealloc);
  893. /*
  894. * Avoid to free 'prealloc' if it can be merged with
  895. * the later extent.
  896. */
  897. err = insert_state(tree, prealloc, start, this_end,
  898. NULL, NULL, &bits, changeset);
  899. if (err)
  900. extent_io_tree_panic(tree, err);
  901. cache_state(prealloc, cached_state);
  902. prealloc = NULL;
  903. start = this_end + 1;
  904. goto search_again;
  905. }
  906. /*
  907. * | ---- desired range ---- |
  908. * | state |
  909. * We need to split the extent, and set the bit
  910. * on the first half
  911. */
  912. if (state->start <= end && state->end > end) {
  913. if (state->state & exclusive_bits) {
  914. *failed_start = start;
  915. err = -EEXIST;
  916. goto out;
  917. }
  918. prealloc = alloc_extent_state_atomic(prealloc);
  919. BUG_ON(!prealloc);
  920. err = split_state(tree, state, prealloc, end + 1);
  921. if (err)
  922. extent_io_tree_panic(tree, err);
  923. set_state_bits(tree, prealloc, &bits, changeset);
  924. cache_state(prealloc, cached_state);
  925. merge_state(tree, prealloc);
  926. prealloc = NULL;
  927. goto out;
  928. }
  929. search_again:
  930. if (start > end)
  931. goto out;
  932. spin_unlock(&tree->lock);
  933. if (gfpflags_allow_blocking(mask))
  934. cond_resched();
  935. goto again;
  936. out:
  937. spin_unlock(&tree->lock);
  938. if (prealloc)
  939. free_extent_state(prealloc);
  940. return err;
  941. }
  942. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  943. unsigned bits, u64 * failed_start,
  944. struct extent_state **cached_state, gfp_t mask)
  945. {
  946. return __set_extent_bit(tree, start, end, bits, 0, failed_start,
  947. cached_state, mask, NULL);
  948. }
  949. /**
  950. * convert_extent_bit - convert all bits in a given range from one bit to
  951. * another
  952. * @tree: the io tree to search
  953. * @start: the start offset in bytes
  954. * @end: the end offset in bytes (inclusive)
  955. * @bits: the bits to set in this range
  956. * @clear_bits: the bits to clear in this range
  957. * @cached_state: state that we're going to cache
  958. *
  959. * This will go through and set bits for the given range. If any states exist
  960. * already in this range they are set with the given bit and cleared of the
  961. * clear_bits. This is only meant to be used by things that are mergeable, ie
  962. * converting from say DELALLOC to DIRTY. This is not meant to be used with
  963. * boundary bits like LOCK.
  964. *
  965. * All allocations are done with GFP_NOFS.
  966. */
  967. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  968. unsigned bits, unsigned clear_bits,
  969. struct extent_state **cached_state)
  970. {
  971. struct extent_state *state;
  972. struct extent_state *prealloc = NULL;
  973. struct rb_node *node;
  974. struct rb_node **p;
  975. struct rb_node *parent;
  976. int err = 0;
  977. u64 last_start;
  978. u64 last_end;
  979. bool first_iteration = true;
  980. btrfs_debug_check_extent_io_range(tree, start, end);
  981. again:
  982. if (!prealloc) {
  983. /*
  984. * Best effort, don't worry if extent state allocation fails
  985. * here for the first iteration. We might have a cached state
  986. * that matches exactly the target range, in which case no
  987. * extent state allocations are needed. We'll only know this
  988. * after locking the tree.
  989. */
  990. prealloc = alloc_extent_state(GFP_NOFS);
  991. if (!prealloc && !first_iteration)
  992. return -ENOMEM;
  993. }
  994. spin_lock(&tree->lock);
  995. if (cached_state && *cached_state) {
  996. state = *cached_state;
  997. if (state->start <= start && state->end > start &&
  998. extent_state_in_tree(state)) {
  999. node = &state->rb_node;
  1000. goto hit_next;
  1001. }
  1002. }
  1003. /*
  1004. * this search will find all the extents that end after
  1005. * our range starts.
  1006. */
  1007. node = tree_search_for_insert(tree, start, &p, &parent);
  1008. if (!node) {
  1009. prealloc = alloc_extent_state_atomic(prealloc);
  1010. if (!prealloc) {
  1011. err = -ENOMEM;
  1012. goto out;
  1013. }
  1014. err = insert_state(tree, prealloc, start, end,
  1015. &p, &parent, &bits, NULL);
  1016. if (err)
  1017. extent_io_tree_panic(tree, err);
  1018. cache_state(prealloc, cached_state);
  1019. prealloc = NULL;
  1020. goto out;
  1021. }
  1022. state = rb_entry(node, struct extent_state, rb_node);
  1023. hit_next:
  1024. last_start = state->start;
  1025. last_end = state->end;
  1026. /*
  1027. * | ---- desired range ---- |
  1028. * | state |
  1029. *
  1030. * Just lock what we found and keep going
  1031. */
  1032. if (state->start == start && state->end <= end) {
  1033. set_state_bits(tree, state, &bits, NULL);
  1034. cache_state(state, cached_state);
  1035. state = clear_state_bit(tree, state, &clear_bits, 0, NULL);
  1036. if (last_end == (u64)-1)
  1037. goto out;
  1038. start = last_end + 1;
  1039. if (start < end && state && state->start == start &&
  1040. !need_resched())
  1041. goto hit_next;
  1042. goto search_again;
  1043. }
  1044. /*
  1045. * | ---- desired range ---- |
  1046. * | state |
  1047. * or
  1048. * | ------------- state -------------- |
  1049. *
  1050. * We need to split the extent we found, and may flip bits on
  1051. * second half.
  1052. *
  1053. * If the extent we found extends past our
  1054. * range, we just split and search again. It'll get split
  1055. * again the next time though.
  1056. *
  1057. * If the extent we found is inside our range, we set the
  1058. * desired bit on it.
  1059. */
  1060. if (state->start < start) {
  1061. prealloc = alloc_extent_state_atomic(prealloc);
  1062. if (!prealloc) {
  1063. err = -ENOMEM;
  1064. goto out;
  1065. }
  1066. err = split_state(tree, state, prealloc, start);
  1067. if (err)
  1068. extent_io_tree_panic(tree, err);
  1069. prealloc = NULL;
  1070. if (err)
  1071. goto out;
  1072. if (state->end <= end) {
  1073. set_state_bits(tree, state, &bits, NULL);
  1074. cache_state(state, cached_state);
  1075. state = clear_state_bit(tree, state, &clear_bits, 0,
  1076. NULL);
  1077. if (last_end == (u64)-1)
  1078. goto out;
  1079. start = last_end + 1;
  1080. if (start < end && state && state->start == start &&
  1081. !need_resched())
  1082. goto hit_next;
  1083. }
  1084. goto search_again;
  1085. }
  1086. /*
  1087. * | ---- desired range ---- |
  1088. * | state | or | state |
  1089. *
  1090. * There's a hole, we need to insert something in it and
  1091. * ignore the extent we found.
  1092. */
  1093. if (state->start > start) {
  1094. u64 this_end;
  1095. if (end < last_start)
  1096. this_end = end;
  1097. else
  1098. this_end = last_start - 1;
  1099. prealloc = alloc_extent_state_atomic(prealloc);
  1100. if (!prealloc) {
  1101. err = -ENOMEM;
  1102. goto out;
  1103. }
  1104. /*
  1105. * Avoid to free 'prealloc' if it can be merged with
  1106. * the later extent.
  1107. */
  1108. err = insert_state(tree, prealloc, start, this_end,
  1109. NULL, NULL, &bits, NULL);
  1110. if (err)
  1111. extent_io_tree_panic(tree, err);
  1112. cache_state(prealloc, cached_state);
  1113. prealloc = NULL;
  1114. start = this_end + 1;
  1115. goto search_again;
  1116. }
  1117. /*
  1118. * | ---- desired range ---- |
  1119. * | state |
  1120. * We need to split the extent, and set the bit
  1121. * on the first half
  1122. */
  1123. if (state->start <= end && state->end > end) {
  1124. prealloc = alloc_extent_state_atomic(prealloc);
  1125. if (!prealloc) {
  1126. err = -ENOMEM;
  1127. goto out;
  1128. }
  1129. err = split_state(tree, state, prealloc, end + 1);
  1130. if (err)
  1131. extent_io_tree_panic(tree, err);
  1132. set_state_bits(tree, prealloc, &bits, NULL);
  1133. cache_state(prealloc, cached_state);
  1134. clear_state_bit(tree, prealloc, &clear_bits, 0, NULL);
  1135. prealloc = NULL;
  1136. goto out;
  1137. }
  1138. search_again:
  1139. if (start > end)
  1140. goto out;
  1141. spin_unlock(&tree->lock);
  1142. cond_resched();
  1143. first_iteration = false;
  1144. goto again;
  1145. out:
  1146. spin_unlock(&tree->lock);
  1147. if (prealloc)
  1148. free_extent_state(prealloc);
  1149. return err;
  1150. }
  1151. /* wrappers around set/clear extent bit */
  1152. int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1153. unsigned bits, struct extent_changeset *changeset)
  1154. {
  1155. /*
  1156. * We don't support EXTENT_LOCKED yet, as current changeset will
  1157. * record any bits changed, so for EXTENT_LOCKED case, it will
  1158. * either fail with -EEXIST or changeset will record the whole
  1159. * range.
  1160. */
  1161. BUG_ON(bits & EXTENT_LOCKED);
  1162. return __set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
  1163. changeset);
  1164. }
  1165. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1166. unsigned bits, int wake, int delete,
  1167. struct extent_state **cached, gfp_t mask)
  1168. {
  1169. return __clear_extent_bit(tree, start, end, bits, wake, delete,
  1170. cached, mask, NULL);
  1171. }
  1172. int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1173. unsigned bits, struct extent_changeset *changeset)
  1174. {
  1175. /*
  1176. * Don't support EXTENT_LOCKED case, same reason as
  1177. * set_record_extent_bits().
  1178. */
  1179. BUG_ON(bits & EXTENT_LOCKED);
  1180. return __clear_extent_bit(tree, start, end, bits, 0, 0, NULL, GFP_NOFS,
  1181. changeset);
  1182. }
  1183. /*
  1184. * either insert or lock state struct between start and end use mask to tell
  1185. * us if waiting is desired.
  1186. */
  1187. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  1188. struct extent_state **cached_state)
  1189. {
  1190. int err;
  1191. u64 failed_start;
  1192. while (1) {
  1193. err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
  1194. EXTENT_LOCKED, &failed_start,
  1195. cached_state, GFP_NOFS, NULL);
  1196. if (err == -EEXIST) {
  1197. wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
  1198. start = failed_start;
  1199. } else
  1200. break;
  1201. WARN_ON(start > end);
  1202. }
  1203. return err;
  1204. }
  1205. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  1206. {
  1207. int err;
  1208. u64 failed_start;
  1209. err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
  1210. &failed_start, NULL, GFP_NOFS, NULL);
  1211. if (err == -EEXIST) {
  1212. if (failed_start > start)
  1213. clear_extent_bit(tree, start, failed_start - 1,
  1214. EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
  1215. return 0;
  1216. }
  1217. return 1;
  1218. }
  1219. void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
  1220. {
  1221. unsigned long index = start >> PAGE_SHIFT;
  1222. unsigned long end_index = end >> PAGE_SHIFT;
  1223. struct page *page;
  1224. while (index <= end_index) {
  1225. page = find_get_page(inode->i_mapping, index);
  1226. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1227. clear_page_dirty_for_io(page);
  1228. put_page(page);
  1229. index++;
  1230. }
  1231. }
  1232. void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
  1233. {
  1234. unsigned long index = start >> PAGE_SHIFT;
  1235. unsigned long end_index = end >> PAGE_SHIFT;
  1236. struct page *page;
  1237. while (index <= end_index) {
  1238. page = find_get_page(inode->i_mapping, index);
  1239. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1240. __set_page_dirty_nobuffers(page);
  1241. account_page_redirty(page);
  1242. put_page(page);
  1243. index++;
  1244. }
  1245. }
  1246. /*
  1247. * helper function to set both pages and extents in the tree writeback
  1248. */
  1249. static void set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
  1250. {
  1251. unsigned long index = start >> PAGE_SHIFT;
  1252. unsigned long end_index = end >> PAGE_SHIFT;
  1253. struct page *page;
  1254. while (index <= end_index) {
  1255. page = find_get_page(tree->mapping, index);
  1256. BUG_ON(!page); /* Pages should be in the extent_io_tree */
  1257. set_page_writeback(page);
  1258. put_page(page);
  1259. index++;
  1260. }
  1261. }
  1262. /* find the first state struct with 'bits' set after 'start', and
  1263. * return it. tree->lock must be held. NULL will returned if
  1264. * nothing was found after 'start'
  1265. */
  1266. static struct extent_state *
  1267. find_first_extent_bit_state(struct extent_io_tree *tree,
  1268. u64 start, unsigned bits)
  1269. {
  1270. struct rb_node *node;
  1271. struct extent_state *state;
  1272. /*
  1273. * this search will find all the extents that end after
  1274. * our range starts.
  1275. */
  1276. node = tree_search(tree, start);
  1277. if (!node)
  1278. goto out;
  1279. while (1) {
  1280. state = rb_entry(node, struct extent_state, rb_node);
  1281. if (state->end >= start && (state->state & bits))
  1282. return state;
  1283. node = rb_next(node);
  1284. if (!node)
  1285. break;
  1286. }
  1287. out:
  1288. return NULL;
  1289. }
  1290. /*
  1291. * find the first offset in the io tree with 'bits' set. zero is
  1292. * returned if we find something, and *start_ret and *end_ret are
  1293. * set to reflect the state struct that was found.
  1294. *
  1295. * If nothing was found, 1 is returned. If found something, return 0.
  1296. */
  1297. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  1298. u64 *start_ret, u64 *end_ret, unsigned bits,
  1299. struct extent_state **cached_state)
  1300. {
  1301. struct extent_state *state;
  1302. struct rb_node *n;
  1303. int ret = 1;
  1304. spin_lock(&tree->lock);
  1305. if (cached_state && *cached_state) {
  1306. state = *cached_state;
  1307. if (state->end == start - 1 && extent_state_in_tree(state)) {
  1308. n = rb_next(&state->rb_node);
  1309. while (n) {
  1310. state = rb_entry(n, struct extent_state,
  1311. rb_node);
  1312. if (state->state & bits)
  1313. goto got_it;
  1314. n = rb_next(n);
  1315. }
  1316. free_extent_state(*cached_state);
  1317. *cached_state = NULL;
  1318. goto out;
  1319. }
  1320. free_extent_state(*cached_state);
  1321. *cached_state = NULL;
  1322. }
  1323. state = find_first_extent_bit_state(tree, start, bits);
  1324. got_it:
  1325. if (state) {
  1326. cache_state_if_flags(state, cached_state, 0);
  1327. *start_ret = state->start;
  1328. *end_ret = state->end;
  1329. ret = 0;
  1330. }
  1331. out:
  1332. spin_unlock(&tree->lock);
  1333. return ret;
  1334. }
  1335. /*
  1336. * find a contiguous range of bytes in the file marked as delalloc, not
  1337. * more than 'max_bytes'. start and end are used to return the range,
  1338. *
  1339. * 1 is returned if we find something, 0 if nothing was in the tree
  1340. */
  1341. static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
  1342. u64 *start, u64 *end, u64 max_bytes,
  1343. struct extent_state **cached_state)
  1344. {
  1345. struct rb_node *node;
  1346. struct extent_state *state;
  1347. u64 cur_start = *start;
  1348. u64 found = 0;
  1349. u64 total_bytes = 0;
  1350. spin_lock(&tree->lock);
  1351. /*
  1352. * this search will find all the extents that end after
  1353. * our range starts.
  1354. */
  1355. node = tree_search(tree, cur_start);
  1356. if (!node) {
  1357. if (!found)
  1358. *end = (u64)-1;
  1359. goto out;
  1360. }
  1361. while (1) {
  1362. state = rb_entry(node, struct extent_state, rb_node);
  1363. if (found && (state->start != cur_start ||
  1364. (state->state & EXTENT_BOUNDARY))) {
  1365. goto out;
  1366. }
  1367. if (!(state->state & EXTENT_DELALLOC)) {
  1368. if (!found)
  1369. *end = state->end;
  1370. goto out;
  1371. }
  1372. if (!found) {
  1373. *start = state->start;
  1374. *cached_state = state;
  1375. refcount_inc(&state->refs);
  1376. }
  1377. found++;
  1378. *end = state->end;
  1379. cur_start = state->end + 1;
  1380. node = rb_next(node);
  1381. total_bytes += state->end - state->start + 1;
  1382. if (total_bytes >= max_bytes)
  1383. break;
  1384. if (!node)
  1385. break;
  1386. }
  1387. out:
  1388. spin_unlock(&tree->lock);
  1389. return found;
  1390. }
  1391. static int __process_pages_contig(struct address_space *mapping,
  1392. struct page *locked_page,
  1393. pgoff_t start_index, pgoff_t end_index,
  1394. unsigned long page_ops, pgoff_t *index_ret);
  1395. static noinline void __unlock_for_delalloc(struct inode *inode,
  1396. struct page *locked_page,
  1397. u64 start, u64 end)
  1398. {
  1399. unsigned long index = start >> PAGE_SHIFT;
  1400. unsigned long end_index = end >> PAGE_SHIFT;
  1401. ASSERT(locked_page);
  1402. if (index == locked_page->index && end_index == index)
  1403. return;
  1404. __process_pages_contig(inode->i_mapping, locked_page, index, end_index,
  1405. PAGE_UNLOCK, NULL);
  1406. }
  1407. static noinline int lock_delalloc_pages(struct inode *inode,
  1408. struct page *locked_page,
  1409. u64 delalloc_start,
  1410. u64 delalloc_end)
  1411. {
  1412. unsigned long index = delalloc_start >> PAGE_SHIFT;
  1413. unsigned long index_ret = index;
  1414. unsigned long end_index = delalloc_end >> PAGE_SHIFT;
  1415. int ret;
  1416. ASSERT(locked_page);
  1417. if (index == locked_page->index && index == end_index)
  1418. return 0;
  1419. ret = __process_pages_contig(inode->i_mapping, locked_page, index,
  1420. end_index, PAGE_LOCK, &index_ret);
  1421. if (ret == -EAGAIN)
  1422. __unlock_for_delalloc(inode, locked_page, delalloc_start,
  1423. (u64)index_ret << PAGE_SHIFT);
  1424. return ret;
  1425. }
  1426. /*
  1427. * find a contiguous range of bytes in the file marked as delalloc, not
  1428. * more than 'max_bytes'. start and end are used to return the range,
  1429. *
  1430. * 1 is returned if we find something, 0 if nothing was in the tree
  1431. */
  1432. STATIC u64 find_lock_delalloc_range(struct inode *inode,
  1433. struct extent_io_tree *tree,
  1434. struct page *locked_page, u64 *start,
  1435. u64 *end, u64 max_bytes)
  1436. {
  1437. u64 delalloc_start;
  1438. u64 delalloc_end;
  1439. u64 found;
  1440. struct extent_state *cached_state = NULL;
  1441. int ret;
  1442. int loops = 0;
  1443. again:
  1444. /* step one, find a bunch of delalloc bytes starting at start */
  1445. delalloc_start = *start;
  1446. delalloc_end = 0;
  1447. found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
  1448. max_bytes, &cached_state);
  1449. if (!found || delalloc_end <= *start) {
  1450. *start = delalloc_start;
  1451. *end = delalloc_end;
  1452. free_extent_state(cached_state);
  1453. return 0;
  1454. }
  1455. /*
  1456. * start comes from the offset of locked_page. We have to lock
  1457. * pages in order, so we can't process delalloc bytes before
  1458. * locked_page
  1459. */
  1460. if (delalloc_start < *start)
  1461. delalloc_start = *start;
  1462. /*
  1463. * make sure to limit the number of pages we try to lock down
  1464. */
  1465. if (delalloc_end + 1 - delalloc_start > max_bytes)
  1466. delalloc_end = delalloc_start + max_bytes - 1;
  1467. /* step two, lock all the pages after the page that has start */
  1468. ret = lock_delalloc_pages(inode, locked_page,
  1469. delalloc_start, delalloc_end);
  1470. if (ret == -EAGAIN) {
  1471. /* some of the pages are gone, lets avoid looping by
  1472. * shortening the size of the delalloc range we're searching
  1473. */
  1474. free_extent_state(cached_state);
  1475. cached_state = NULL;
  1476. if (!loops) {
  1477. max_bytes = PAGE_SIZE;
  1478. loops = 1;
  1479. goto again;
  1480. } else {
  1481. found = 0;
  1482. goto out_failed;
  1483. }
  1484. }
  1485. BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
  1486. /* step three, lock the state bits for the whole range */
  1487. lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
  1488. /* then test to make sure it is all still delalloc */
  1489. ret = test_range_bit(tree, delalloc_start, delalloc_end,
  1490. EXTENT_DELALLOC, 1, cached_state);
  1491. if (!ret) {
  1492. unlock_extent_cached(tree, delalloc_start, delalloc_end,
  1493. &cached_state, GFP_NOFS);
  1494. __unlock_for_delalloc(inode, locked_page,
  1495. delalloc_start, delalloc_end);
  1496. cond_resched();
  1497. goto again;
  1498. }
  1499. free_extent_state(cached_state);
  1500. *start = delalloc_start;
  1501. *end = delalloc_end;
  1502. out_failed:
  1503. return found;
  1504. }
  1505. static int __process_pages_contig(struct address_space *mapping,
  1506. struct page *locked_page,
  1507. pgoff_t start_index, pgoff_t end_index,
  1508. unsigned long page_ops, pgoff_t *index_ret)
  1509. {
  1510. unsigned long nr_pages = end_index - start_index + 1;
  1511. unsigned long pages_locked = 0;
  1512. pgoff_t index = start_index;
  1513. struct page *pages[16];
  1514. unsigned ret;
  1515. int err = 0;
  1516. int i;
  1517. if (page_ops & PAGE_LOCK) {
  1518. ASSERT(page_ops == PAGE_LOCK);
  1519. ASSERT(index_ret && *index_ret == start_index);
  1520. }
  1521. if ((page_ops & PAGE_SET_ERROR) && nr_pages > 0)
  1522. mapping_set_error(mapping, -EIO);
  1523. while (nr_pages > 0) {
  1524. ret = find_get_pages_contig(mapping, index,
  1525. min_t(unsigned long,
  1526. nr_pages, ARRAY_SIZE(pages)), pages);
  1527. if (ret == 0) {
  1528. /*
  1529. * Only if we're going to lock these pages,
  1530. * can we find nothing at @index.
  1531. */
  1532. ASSERT(page_ops & PAGE_LOCK);
  1533. err = -EAGAIN;
  1534. goto out;
  1535. }
  1536. for (i = 0; i < ret; i++) {
  1537. if (page_ops & PAGE_SET_PRIVATE2)
  1538. SetPagePrivate2(pages[i]);
  1539. if (pages[i] == locked_page) {
  1540. put_page(pages[i]);
  1541. pages_locked++;
  1542. continue;
  1543. }
  1544. if (page_ops & PAGE_CLEAR_DIRTY)
  1545. clear_page_dirty_for_io(pages[i]);
  1546. if (page_ops & PAGE_SET_WRITEBACK)
  1547. set_page_writeback(pages[i]);
  1548. if (page_ops & PAGE_SET_ERROR)
  1549. SetPageError(pages[i]);
  1550. if (page_ops & PAGE_END_WRITEBACK)
  1551. end_page_writeback(pages[i]);
  1552. if (page_ops & PAGE_UNLOCK)
  1553. unlock_page(pages[i]);
  1554. if (page_ops & PAGE_LOCK) {
  1555. lock_page(pages[i]);
  1556. if (!PageDirty(pages[i]) ||
  1557. pages[i]->mapping != mapping) {
  1558. unlock_page(pages[i]);
  1559. put_page(pages[i]);
  1560. err = -EAGAIN;
  1561. goto out;
  1562. }
  1563. }
  1564. put_page(pages[i]);
  1565. pages_locked++;
  1566. }
  1567. nr_pages -= ret;
  1568. index += ret;
  1569. cond_resched();
  1570. }
  1571. out:
  1572. if (err && index_ret)
  1573. *index_ret = start_index + pages_locked - 1;
  1574. return err;
  1575. }
  1576. void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
  1577. u64 delalloc_end, struct page *locked_page,
  1578. unsigned clear_bits,
  1579. unsigned long page_ops)
  1580. {
  1581. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
  1582. NULL, GFP_NOFS);
  1583. __process_pages_contig(inode->i_mapping, locked_page,
  1584. start >> PAGE_SHIFT, end >> PAGE_SHIFT,
  1585. page_ops, NULL);
  1586. }
  1587. /*
  1588. * count the number of bytes in the tree that have a given bit(s)
  1589. * set. This can be fairly slow, except for EXTENT_DIRTY which is
  1590. * cached. The total number found is returned.
  1591. */
  1592. u64 count_range_bits(struct extent_io_tree *tree,
  1593. u64 *start, u64 search_end, u64 max_bytes,
  1594. unsigned bits, int contig)
  1595. {
  1596. struct rb_node *node;
  1597. struct extent_state *state;
  1598. u64 cur_start = *start;
  1599. u64 total_bytes = 0;
  1600. u64 last = 0;
  1601. int found = 0;
  1602. if (WARN_ON(search_end <= cur_start))
  1603. return 0;
  1604. spin_lock(&tree->lock);
  1605. if (cur_start == 0 && bits == EXTENT_DIRTY) {
  1606. total_bytes = tree->dirty_bytes;
  1607. goto out;
  1608. }
  1609. /*
  1610. * this search will find all the extents that end after
  1611. * our range starts.
  1612. */
  1613. node = tree_search(tree, cur_start);
  1614. if (!node)
  1615. goto out;
  1616. while (1) {
  1617. state = rb_entry(node, struct extent_state, rb_node);
  1618. if (state->start > search_end)
  1619. break;
  1620. if (contig && found && state->start > last + 1)
  1621. break;
  1622. if (state->end >= cur_start && (state->state & bits) == bits) {
  1623. total_bytes += min(search_end, state->end) + 1 -
  1624. max(cur_start, state->start);
  1625. if (total_bytes >= max_bytes)
  1626. break;
  1627. if (!found) {
  1628. *start = max(cur_start, state->start);
  1629. found = 1;
  1630. }
  1631. last = state->end;
  1632. } else if (contig && found) {
  1633. break;
  1634. }
  1635. node = rb_next(node);
  1636. if (!node)
  1637. break;
  1638. }
  1639. out:
  1640. spin_unlock(&tree->lock);
  1641. return total_bytes;
  1642. }
  1643. /*
  1644. * set the private field for a given byte offset in the tree. If there isn't
  1645. * an extent_state there already, this does nothing.
  1646. */
  1647. static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
  1648. struct io_failure_record *failrec)
  1649. {
  1650. struct rb_node *node;
  1651. struct extent_state *state;
  1652. int ret = 0;
  1653. spin_lock(&tree->lock);
  1654. /*
  1655. * this search will find all the extents that end after
  1656. * our range starts.
  1657. */
  1658. node = tree_search(tree, start);
  1659. if (!node) {
  1660. ret = -ENOENT;
  1661. goto out;
  1662. }
  1663. state = rb_entry(node, struct extent_state, rb_node);
  1664. if (state->start != start) {
  1665. ret = -ENOENT;
  1666. goto out;
  1667. }
  1668. state->failrec = failrec;
  1669. out:
  1670. spin_unlock(&tree->lock);
  1671. return ret;
  1672. }
  1673. static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
  1674. struct io_failure_record **failrec)
  1675. {
  1676. struct rb_node *node;
  1677. struct extent_state *state;
  1678. int ret = 0;
  1679. spin_lock(&tree->lock);
  1680. /*
  1681. * this search will find all the extents that end after
  1682. * our range starts.
  1683. */
  1684. node = tree_search(tree, start);
  1685. if (!node) {
  1686. ret = -ENOENT;
  1687. goto out;
  1688. }
  1689. state = rb_entry(node, struct extent_state, rb_node);
  1690. if (state->start != start) {
  1691. ret = -ENOENT;
  1692. goto out;
  1693. }
  1694. *failrec = state->failrec;
  1695. out:
  1696. spin_unlock(&tree->lock);
  1697. return ret;
  1698. }
  1699. /*
  1700. * searches a range in the state tree for a given mask.
  1701. * If 'filled' == 1, this returns 1 only if every extent in the tree
  1702. * has the bits set. Otherwise, 1 is returned if any bit in the
  1703. * range is found set.
  1704. */
  1705. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1706. unsigned bits, int filled, struct extent_state *cached)
  1707. {
  1708. struct extent_state *state = NULL;
  1709. struct rb_node *node;
  1710. int bitset = 0;
  1711. spin_lock(&tree->lock);
  1712. if (cached && extent_state_in_tree(cached) && cached->start <= start &&
  1713. cached->end > start)
  1714. node = &cached->rb_node;
  1715. else
  1716. node = tree_search(tree, start);
  1717. while (node && start <= end) {
  1718. state = rb_entry(node, struct extent_state, rb_node);
  1719. if (filled && state->start > start) {
  1720. bitset = 0;
  1721. break;
  1722. }
  1723. if (state->start > end)
  1724. break;
  1725. if (state->state & bits) {
  1726. bitset = 1;
  1727. if (!filled)
  1728. break;
  1729. } else if (filled) {
  1730. bitset = 0;
  1731. break;
  1732. }
  1733. if (state->end == (u64)-1)
  1734. break;
  1735. start = state->end + 1;
  1736. if (start > end)
  1737. break;
  1738. node = rb_next(node);
  1739. if (!node) {
  1740. if (filled)
  1741. bitset = 0;
  1742. break;
  1743. }
  1744. }
  1745. spin_unlock(&tree->lock);
  1746. return bitset;
  1747. }
  1748. /*
  1749. * helper function to set a given page up to date if all the
  1750. * extents in the tree for that page are up to date
  1751. */
  1752. static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
  1753. {
  1754. u64 start = page_offset(page);
  1755. u64 end = start + PAGE_SIZE - 1;
  1756. if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
  1757. SetPageUptodate(page);
  1758. }
  1759. int free_io_failure(struct btrfs_inode *inode, struct io_failure_record *rec)
  1760. {
  1761. int ret;
  1762. int err = 0;
  1763. struct extent_io_tree *failure_tree = &inode->io_failure_tree;
  1764. set_state_failrec(failure_tree, rec->start, NULL);
  1765. ret = clear_extent_bits(failure_tree, rec->start,
  1766. rec->start + rec->len - 1,
  1767. EXTENT_LOCKED | EXTENT_DIRTY);
  1768. if (ret)
  1769. err = ret;
  1770. ret = clear_extent_bits(&inode->io_tree, rec->start,
  1771. rec->start + rec->len - 1,
  1772. EXTENT_DAMAGED);
  1773. if (ret && !err)
  1774. err = ret;
  1775. kfree(rec);
  1776. return err;
  1777. }
  1778. /*
  1779. * this bypasses the standard btrfs submit functions deliberately, as
  1780. * the standard behavior is to write all copies in a raid setup. here we only
  1781. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1782. * submit_bio directly.
  1783. * to avoid any synchronization issues, wait for the data after writing, which
  1784. * actually prevents the read that triggered the error from finishing.
  1785. * currently, there can be no more than two copies of every data bit. thus,
  1786. * exactly one rewrite is required.
  1787. */
  1788. int repair_io_failure(struct btrfs_inode *inode, u64 start, u64 length,
  1789. u64 logical, struct page *page,
  1790. unsigned int pg_offset, int mirror_num)
  1791. {
  1792. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1793. struct bio *bio;
  1794. struct btrfs_device *dev;
  1795. u64 map_length = 0;
  1796. u64 sector;
  1797. struct btrfs_bio *bbio = NULL;
  1798. int ret;
  1799. ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
  1800. BUG_ON(!mirror_num);
  1801. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  1802. if (!bio)
  1803. return -EIO;
  1804. bio->bi_iter.bi_size = 0;
  1805. map_length = length;
  1806. /*
  1807. * Avoid races with device replace and make sure our bbio has devices
  1808. * associated to its stripes that don't go away while we are doing the
  1809. * read repair operation.
  1810. */
  1811. btrfs_bio_counter_inc_blocked(fs_info);
  1812. if (btrfs_is_parity_mirror(fs_info, logical, length, mirror_num)) {
  1813. /*
  1814. * Note that we don't use BTRFS_MAP_WRITE because it's supposed
  1815. * to update all raid stripes, but here we just want to correct
  1816. * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
  1817. * stripe's dev and sector.
  1818. */
  1819. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
  1820. &map_length, &bbio, 0);
  1821. if (ret) {
  1822. btrfs_bio_counter_dec(fs_info);
  1823. bio_put(bio);
  1824. return -EIO;
  1825. }
  1826. ASSERT(bbio->mirror_num == 1);
  1827. } else {
  1828. ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
  1829. &map_length, &bbio, mirror_num);
  1830. if (ret) {
  1831. btrfs_bio_counter_dec(fs_info);
  1832. bio_put(bio);
  1833. return -EIO;
  1834. }
  1835. BUG_ON(mirror_num != bbio->mirror_num);
  1836. }
  1837. sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
  1838. bio->bi_iter.bi_sector = sector;
  1839. dev = bbio->stripes[bbio->mirror_num - 1].dev;
  1840. btrfs_put_bbio(bbio);
  1841. if (!dev || !dev->bdev || !dev->writeable) {
  1842. btrfs_bio_counter_dec(fs_info);
  1843. bio_put(bio);
  1844. return -EIO;
  1845. }
  1846. bio->bi_bdev = dev->bdev;
  1847. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
  1848. bio_add_page(bio, page, length, pg_offset);
  1849. if (btrfsic_submit_bio_wait(bio)) {
  1850. /* try to remap that extent elsewhere? */
  1851. btrfs_bio_counter_dec(fs_info);
  1852. bio_put(bio);
  1853. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1854. return -EIO;
  1855. }
  1856. btrfs_info_rl_in_rcu(fs_info,
  1857. "read error corrected: ino %llu off %llu (dev %s sector %llu)",
  1858. btrfs_ino(inode), start,
  1859. rcu_str_deref(dev->name), sector);
  1860. btrfs_bio_counter_dec(fs_info);
  1861. bio_put(bio);
  1862. return 0;
  1863. }
  1864. int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
  1865. struct extent_buffer *eb, int mirror_num)
  1866. {
  1867. u64 start = eb->start;
  1868. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1869. int ret = 0;
  1870. if (fs_info->sb->s_flags & MS_RDONLY)
  1871. return -EROFS;
  1872. for (i = 0; i < num_pages; i++) {
  1873. struct page *p = eb->pages[i];
  1874. ret = repair_io_failure(BTRFS_I(fs_info->btree_inode), start,
  1875. PAGE_SIZE, start, p,
  1876. start - page_offset(p), mirror_num);
  1877. if (ret)
  1878. break;
  1879. start += PAGE_SIZE;
  1880. }
  1881. return ret;
  1882. }
  1883. /*
  1884. * each time an IO finishes, we do a fast check in the IO failure tree
  1885. * to see if we need to process or clean up an io_failure_record
  1886. */
  1887. int clean_io_failure(struct btrfs_inode *inode, u64 start, struct page *page,
  1888. unsigned int pg_offset)
  1889. {
  1890. u64 private;
  1891. struct io_failure_record *failrec;
  1892. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1893. struct extent_state *state;
  1894. int num_copies;
  1895. int ret;
  1896. private = 0;
  1897. ret = count_range_bits(&inode->io_failure_tree, &private,
  1898. (u64)-1, 1, EXTENT_DIRTY, 0);
  1899. if (!ret)
  1900. return 0;
  1901. ret = get_state_failrec(&inode->io_failure_tree, start,
  1902. &failrec);
  1903. if (ret)
  1904. return 0;
  1905. BUG_ON(!failrec->this_mirror);
  1906. if (failrec->in_validation) {
  1907. /* there was no real error, just free the record */
  1908. btrfs_debug(fs_info,
  1909. "clean_io_failure: freeing dummy error at %llu",
  1910. failrec->start);
  1911. goto out;
  1912. }
  1913. if (fs_info->sb->s_flags & MS_RDONLY)
  1914. goto out;
  1915. spin_lock(&inode->io_tree.lock);
  1916. state = find_first_extent_bit_state(&inode->io_tree,
  1917. failrec->start,
  1918. EXTENT_LOCKED);
  1919. spin_unlock(&inode->io_tree.lock);
  1920. if (state && state->start <= failrec->start &&
  1921. state->end >= failrec->start + failrec->len - 1) {
  1922. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1923. failrec->len);
  1924. if (num_copies > 1) {
  1925. repair_io_failure(inode, start, failrec->len,
  1926. failrec->logical, page,
  1927. pg_offset, failrec->failed_mirror);
  1928. }
  1929. }
  1930. out:
  1931. free_io_failure(inode, failrec);
  1932. return 0;
  1933. }
  1934. /*
  1935. * Can be called when
  1936. * - hold extent lock
  1937. * - under ordered extent
  1938. * - the inode is freeing
  1939. */
  1940. void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
  1941. {
  1942. struct extent_io_tree *failure_tree = &inode->io_failure_tree;
  1943. struct io_failure_record *failrec;
  1944. struct extent_state *state, *next;
  1945. if (RB_EMPTY_ROOT(&failure_tree->state))
  1946. return;
  1947. spin_lock(&failure_tree->lock);
  1948. state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
  1949. while (state) {
  1950. if (state->start > end)
  1951. break;
  1952. ASSERT(state->end <= end);
  1953. next = next_state(state);
  1954. failrec = state->failrec;
  1955. free_extent_state(state);
  1956. kfree(failrec);
  1957. state = next;
  1958. }
  1959. spin_unlock(&failure_tree->lock);
  1960. }
  1961. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  1962. struct io_failure_record **failrec_ret)
  1963. {
  1964. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1965. struct io_failure_record *failrec;
  1966. struct extent_map *em;
  1967. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1968. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1969. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1970. int ret;
  1971. u64 logical;
  1972. ret = get_state_failrec(failure_tree, start, &failrec);
  1973. if (ret) {
  1974. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1975. if (!failrec)
  1976. return -ENOMEM;
  1977. failrec->start = start;
  1978. failrec->len = end - start + 1;
  1979. failrec->this_mirror = 0;
  1980. failrec->bio_flags = 0;
  1981. failrec->in_validation = 0;
  1982. read_lock(&em_tree->lock);
  1983. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1984. if (!em) {
  1985. read_unlock(&em_tree->lock);
  1986. kfree(failrec);
  1987. return -EIO;
  1988. }
  1989. if (em->start > start || em->start + em->len <= start) {
  1990. free_extent_map(em);
  1991. em = NULL;
  1992. }
  1993. read_unlock(&em_tree->lock);
  1994. if (!em) {
  1995. kfree(failrec);
  1996. return -EIO;
  1997. }
  1998. logical = start - em->start;
  1999. logical = em->block_start + logical;
  2000. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2001. logical = em->block_start;
  2002. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  2003. extent_set_compress_type(&failrec->bio_flags,
  2004. em->compress_type);
  2005. }
  2006. btrfs_debug(fs_info,
  2007. "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
  2008. logical, start, failrec->len);
  2009. failrec->logical = logical;
  2010. free_extent_map(em);
  2011. /* set the bits in the private failure tree */
  2012. ret = set_extent_bits(failure_tree, start, end,
  2013. EXTENT_LOCKED | EXTENT_DIRTY);
  2014. if (ret >= 0)
  2015. ret = set_state_failrec(failure_tree, start, failrec);
  2016. /* set the bits in the inode's tree */
  2017. if (ret >= 0)
  2018. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
  2019. if (ret < 0) {
  2020. kfree(failrec);
  2021. return ret;
  2022. }
  2023. } else {
  2024. btrfs_debug(fs_info,
  2025. "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
  2026. failrec->logical, failrec->start, failrec->len,
  2027. failrec->in_validation);
  2028. /*
  2029. * when data can be on disk more than twice, add to failrec here
  2030. * (e.g. with a list for failed_mirror) to make
  2031. * clean_io_failure() clean all those errors at once.
  2032. */
  2033. }
  2034. *failrec_ret = failrec;
  2035. return 0;
  2036. }
  2037. int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  2038. struct io_failure_record *failrec, int failed_mirror)
  2039. {
  2040. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2041. int num_copies;
  2042. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  2043. if (num_copies == 1) {
  2044. /*
  2045. * we only have a single copy of the data, so don't bother with
  2046. * all the retry and error correction code that follows. no
  2047. * matter what the error is, it is very likely to persist.
  2048. */
  2049. btrfs_debug(fs_info,
  2050. "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  2051. num_copies, failrec->this_mirror, failed_mirror);
  2052. return 0;
  2053. }
  2054. /*
  2055. * there are two premises:
  2056. * a) deliver good data to the caller
  2057. * b) correct the bad sectors on disk
  2058. */
  2059. if (failed_bio->bi_vcnt > 1) {
  2060. /*
  2061. * to fulfill b), we need to know the exact failing sectors, as
  2062. * we don't want to rewrite any more than the failed ones. thus,
  2063. * we need separate read requests for the failed bio
  2064. *
  2065. * if the following BUG_ON triggers, our validation request got
  2066. * merged. we need separate requests for our algorithm to work.
  2067. */
  2068. BUG_ON(failrec->in_validation);
  2069. failrec->in_validation = 1;
  2070. failrec->this_mirror = failed_mirror;
  2071. } else {
  2072. /*
  2073. * we're ready to fulfill a) and b) alongside. get a good copy
  2074. * of the failed sector and if we succeed, we have setup
  2075. * everything for repair_io_failure to do the rest for us.
  2076. */
  2077. if (failrec->in_validation) {
  2078. BUG_ON(failrec->this_mirror != failed_mirror);
  2079. failrec->in_validation = 0;
  2080. failrec->this_mirror = 0;
  2081. }
  2082. failrec->failed_mirror = failed_mirror;
  2083. failrec->this_mirror++;
  2084. if (failrec->this_mirror == failed_mirror)
  2085. failrec->this_mirror++;
  2086. }
  2087. if (failrec->this_mirror > num_copies) {
  2088. btrfs_debug(fs_info,
  2089. "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  2090. num_copies, failrec->this_mirror, failed_mirror);
  2091. return 0;
  2092. }
  2093. return 1;
  2094. }
  2095. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  2096. struct io_failure_record *failrec,
  2097. struct page *page, int pg_offset, int icsum,
  2098. bio_end_io_t *endio_func, void *data)
  2099. {
  2100. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2101. struct bio *bio;
  2102. struct btrfs_io_bio *btrfs_failed_bio;
  2103. struct btrfs_io_bio *btrfs_bio;
  2104. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  2105. if (!bio)
  2106. return NULL;
  2107. bio->bi_end_io = endio_func;
  2108. bio->bi_iter.bi_sector = failrec->logical >> 9;
  2109. bio->bi_bdev = fs_info->fs_devices->latest_bdev;
  2110. bio->bi_iter.bi_size = 0;
  2111. bio->bi_private = data;
  2112. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2113. if (btrfs_failed_bio->csum) {
  2114. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2115. btrfs_bio = btrfs_io_bio(bio);
  2116. btrfs_bio->csum = btrfs_bio->csum_inline;
  2117. icsum *= csum_size;
  2118. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
  2119. csum_size);
  2120. }
  2121. bio_add_page(bio, page, failrec->len, pg_offset);
  2122. return bio;
  2123. }
  2124. /*
  2125. * this is a generic handler for readpage errors (default
  2126. * readpage_io_failed_hook). if other copies exist, read those and write back
  2127. * good data to the failed position. does not investigate in remapping the
  2128. * failed extent elsewhere, hoping the device will be smart enough to do this as
  2129. * needed
  2130. */
  2131. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  2132. struct page *page, u64 start, u64 end,
  2133. int failed_mirror)
  2134. {
  2135. struct io_failure_record *failrec;
  2136. struct inode *inode = page->mapping->host;
  2137. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2138. struct bio *bio;
  2139. int read_mode = 0;
  2140. blk_status_t status;
  2141. int ret;
  2142. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  2143. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  2144. if (ret)
  2145. return ret;
  2146. ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
  2147. if (!ret) {
  2148. free_io_failure(BTRFS_I(inode), failrec);
  2149. return -EIO;
  2150. }
  2151. if (failed_bio->bi_vcnt > 1)
  2152. read_mode |= REQ_FAILFAST_DEV;
  2153. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2154. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  2155. start - page_offset(page),
  2156. (int)phy_offset, failed_bio->bi_end_io,
  2157. NULL);
  2158. if (!bio) {
  2159. free_io_failure(BTRFS_I(inode), failrec);
  2160. return -EIO;
  2161. }
  2162. bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
  2163. btrfs_debug(btrfs_sb(inode->i_sb),
  2164. "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
  2165. read_mode, failrec->this_mirror, failrec->in_validation);
  2166. status = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror,
  2167. failrec->bio_flags, 0);
  2168. if (status) {
  2169. free_io_failure(BTRFS_I(inode), failrec);
  2170. bio_put(bio);
  2171. ret = blk_status_to_errno(status);
  2172. }
  2173. return ret;
  2174. }
  2175. /* lots and lots of room for performance fixes in the end_bio funcs */
  2176. void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2177. {
  2178. int uptodate = (err == 0);
  2179. struct extent_io_tree *tree;
  2180. int ret = 0;
  2181. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2182. if (tree->ops && tree->ops->writepage_end_io_hook)
  2183. tree->ops->writepage_end_io_hook(page, start, end, NULL,
  2184. uptodate);
  2185. if (!uptodate) {
  2186. ClearPageUptodate(page);
  2187. SetPageError(page);
  2188. ret = err < 0 ? err : -EIO;
  2189. mapping_set_error(page->mapping, ret);
  2190. }
  2191. }
  2192. /*
  2193. * after a writepage IO is done, we need to:
  2194. * clear the uptodate bits on error
  2195. * clear the writeback bits in the extent tree for this IO
  2196. * end_page_writeback if the page has no more pending IO
  2197. *
  2198. * Scheduling is not allowed, so the extent state tree is expected
  2199. * to have one and only one object corresponding to this IO.
  2200. */
  2201. static void end_bio_extent_writepage(struct bio *bio)
  2202. {
  2203. int error = blk_status_to_errno(bio->bi_status);
  2204. struct bio_vec *bvec;
  2205. u64 start;
  2206. u64 end;
  2207. int i;
  2208. bio_for_each_segment_all(bvec, bio, i) {
  2209. struct page *page = bvec->bv_page;
  2210. struct inode *inode = page->mapping->host;
  2211. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2212. /* We always issue full-page reads, but if some block
  2213. * in a page fails to read, blk_update_request() will
  2214. * advance bv_offset and adjust bv_len to compensate.
  2215. * Print a warning for nonzero offsets, and an error
  2216. * if they don't add up to a full page. */
  2217. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2218. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2219. btrfs_err(fs_info,
  2220. "partial page write in btrfs with offset %u and length %u",
  2221. bvec->bv_offset, bvec->bv_len);
  2222. else
  2223. btrfs_info(fs_info,
  2224. "incomplete page write in btrfs with offset %u and length %u",
  2225. bvec->bv_offset, bvec->bv_len);
  2226. }
  2227. start = page_offset(page);
  2228. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2229. end_extent_writepage(page, error, start, end);
  2230. end_page_writeback(page);
  2231. }
  2232. bio_put(bio);
  2233. }
  2234. static void
  2235. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2236. int uptodate)
  2237. {
  2238. struct extent_state *cached = NULL;
  2239. u64 end = start + len - 1;
  2240. if (uptodate && tree->track_uptodate)
  2241. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2242. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2243. }
  2244. /*
  2245. * after a readpage IO is done, we need to:
  2246. * clear the uptodate bits on error
  2247. * set the uptodate bits if things worked
  2248. * set the page up to date if all extents in the tree are uptodate
  2249. * clear the lock bit in the extent tree
  2250. * unlock the page if there are no other extents locked for it
  2251. *
  2252. * Scheduling is not allowed, so the extent state tree is expected
  2253. * to have one and only one object corresponding to this IO.
  2254. */
  2255. static void end_bio_extent_readpage(struct bio *bio)
  2256. {
  2257. struct bio_vec *bvec;
  2258. int uptodate = !bio->bi_status;
  2259. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2260. struct extent_io_tree *tree;
  2261. u64 offset = 0;
  2262. u64 start;
  2263. u64 end;
  2264. u64 len;
  2265. u64 extent_start = 0;
  2266. u64 extent_len = 0;
  2267. int mirror;
  2268. int ret;
  2269. int i;
  2270. bio_for_each_segment_all(bvec, bio, i) {
  2271. struct page *page = bvec->bv_page;
  2272. struct inode *inode = page->mapping->host;
  2273. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2274. btrfs_debug(fs_info,
  2275. "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
  2276. (u64)bio->bi_iter.bi_sector, bio->bi_status,
  2277. io_bio->mirror_num);
  2278. tree = &BTRFS_I(inode)->io_tree;
  2279. /* We always issue full-page reads, but if some block
  2280. * in a page fails to read, blk_update_request() will
  2281. * advance bv_offset and adjust bv_len to compensate.
  2282. * Print a warning for nonzero offsets, and an error
  2283. * if they don't add up to a full page. */
  2284. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2285. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2286. btrfs_err(fs_info,
  2287. "partial page read in btrfs with offset %u and length %u",
  2288. bvec->bv_offset, bvec->bv_len);
  2289. else
  2290. btrfs_info(fs_info,
  2291. "incomplete page read in btrfs with offset %u and length %u",
  2292. bvec->bv_offset, bvec->bv_len);
  2293. }
  2294. start = page_offset(page);
  2295. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2296. len = bvec->bv_len;
  2297. mirror = io_bio->mirror_num;
  2298. if (likely(uptodate && tree->ops)) {
  2299. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2300. page, start, end,
  2301. mirror);
  2302. if (ret)
  2303. uptodate = 0;
  2304. else
  2305. clean_io_failure(BTRFS_I(inode), start,
  2306. page, 0);
  2307. }
  2308. if (likely(uptodate))
  2309. goto readpage_ok;
  2310. if (tree->ops) {
  2311. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2312. if (ret == -EAGAIN) {
  2313. /*
  2314. * Data inode's readpage_io_failed_hook() always
  2315. * returns -EAGAIN.
  2316. *
  2317. * The generic bio_readpage_error handles errors
  2318. * the following way: If possible, new read
  2319. * requests are created and submitted and will
  2320. * end up in end_bio_extent_readpage as well (if
  2321. * we're lucky, not in the !uptodate case). In
  2322. * that case it returns 0 and we just go on with
  2323. * the next page in our bio. If it can't handle
  2324. * the error it will return -EIO and we remain
  2325. * responsible for that page.
  2326. */
  2327. ret = bio_readpage_error(bio, offset, page,
  2328. start, end, mirror);
  2329. if (ret == 0) {
  2330. uptodate = !bio->bi_status;
  2331. offset += len;
  2332. continue;
  2333. }
  2334. }
  2335. /*
  2336. * metadata's readpage_io_failed_hook() always returns
  2337. * -EIO and fixes nothing. -EIO is also returned if
  2338. * data inode error could not be fixed.
  2339. */
  2340. ASSERT(ret == -EIO);
  2341. }
  2342. readpage_ok:
  2343. if (likely(uptodate)) {
  2344. loff_t i_size = i_size_read(inode);
  2345. pgoff_t end_index = i_size >> PAGE_SHIFT;
  2346. unsigned off;
  2347. /* Zero out the end if this page straddles i_size */
  2348. off = i_size & (PAGE_SIZE-1);
  2349. if (page->index == end_index && off)
  2350. zero_user_segment(page, off, PAGE_SIZE);
  2351. SetPageUptodate(page);
  2352. } else {
  2353. ClearPageUptodate(page);
  2354. SetPageError(page);
  2355. }
  2356. unlock_page(page);
  2357. offset += len;
  2358. if (unlikely(!uptodate)) {
  2359. if (extent_len) {
  2360. endio_readpage_release_extent(tree,
  2361. extent_start,
  2362. extent_len, 1);
  2363. extent_start = 0;
  2364. extent_len = 0;
  2365. }
  2366. endio_readpage_release_extent(tree, start,
  2367. end - start + 1, 0);
  2368. } else if (!extent_len) {
  2369. extent_start = start;
  2370. extent_len = end + 1 - start;
  2371. } else if (extent_start + extent_len == start) {
  2372. extent_len += end + 1 - start;
  2373. } else {
  2374. endio_readpage_release_extent(tree, extent_start,
  2375. extent_len, uptodate);
  2376. extent_start = start;
  2377. extent_len = end + 1 - start;
  2378. }
  2379. }
  2380. if (extent_len)
  2381. endio_readpage_release_extent(tree, extent_start, extent_len,
  2382. uptodate);
  2383. if (io_bio->end_io)
  2384. io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
  2385. bio_put(bio);
  2386. }
  2387. /*
  2388. * this allocates from the btrfs_bioset. We're returning a bio right now
  2389. * but you can call btrfs_io_bio for the appropriate container_of magic
  2390. */
  2391. struct bio *
  2392. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2393. gfp_t gfp_flags)
  2394. {
  2395. struct btrfs_io_bio *btrfs_bio;
  2396. struct bio *bio;
  2397. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2398. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2399. while (!bio && (nr_vecs /= 2)) {
  2400. bio = bio_alloc_bioset(gfp_flags,
  2401. nr_vecs, btrfs_bioset);
  2402. }
  2403. }
  2404. if (bio) {
  2405. bio->bi_bdev = bdev;
  2406. bio->bi_iter.bi_sector = first_sector;
  2407. btrfs_bio = btrfs_io_bio(bio);
  2408. btrfs_bio->csum = NULL;
  2409. btrfs_bio->csum_allocated = NULL;
  2410. btrfs_bio->end_io = NULL;
  2411. }
  2412. return bio;
  2413. }
  2414. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2415. {
  2416. struct btrfs_io_bio *btrfs_bio;
  2417. struct bio *new;
  2418. new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
  2419. if (new) {
  2420. btrfs_bio = btrfs_io_bio(new);
  2421. btrfs_bio->csum = NULL;
  2422. btrfs_bio->csum_allocated = NULL;
  2423. btrfs_bio->end_io = NULL;
  2424. }
  2425. return new;
  2426. }
  2427. /* this also allocates from the btrfs_bioset */
  2428. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2429. {
  2430. struct btrfs_io_bio *btrfs_bio;
  2431. struct bio *bio;
  2432. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2433. if (bio) {
  2434. btrfs_bio = btrfs_io_bio(bio);
  2435. btrfs_bio->csum = NULL;
  2436. btrfs_bio->csum_allocated = NULL;
  2437. btrfs_bio->end_io = NULL;
  2438. }
  2439. return bio;
  2440. }
  2441. static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
  2442. unsigned long bio_flags)
  2443. {
  2444. blk_status_t ret = 0;
  2445. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2446. struct page *page = bvec->bv_page;
  2447. struct extent_io_tree *tree = bio->bi_private;
  2448. u64 start;
  2449. start = page_offset(page) + bvec->bv_offset;
  2450. bio->bi_private = NULL;
  2451. bio_get(bio);
  2452. if (tree->ops)
  2453. ret = tree->ops->submit_bio_hook(page->mapping->host, bio,
  2454. mirror_num, bio_flags, start);
  2455. else
  2456. btrfsic_submit_bio(bio);
  2457. bio_put(bio);
  2458. return blk_status_to_errno(ret);
  2459. }
  2460. static int merge_bio(struct extent_io_tree *tree, struct page *page,
  2461. unsigned long offset, size_t size, struct bio *bio,
  2462. unsigned long bio_flags)
  2463. {
  2464. int ret = 0;
  2465. if (tree->ops)
  2466. ret = tree->ops->merge_bio_hook(page, offset, size, bio,
  2467. bio_flags);
  2468. return ret;
  2469. }
  2470. static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree,
  2471. struct writeback_control *wbc,
  2472. struct page *page, sector_t sector,
  2473. size_t size, unsigned long offset,
  2474. struct block_device *bdev,
  2475. struct bio **bio_ret,
  2476. bio_end_io_t end_io_func,
  2477. int mirror_num,
  2478. unsigned long prev_bio_flags,
  2479. unsigned long bio_flags,
  2480. bool force_bio_submit)
  2481. {
  2482. int ret = 0;
  2483. struct bio *bio;
  2484. int contig = 0;
  2485. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2486. size_t page_size = min_t(size_t, size, PAGE_SIZE);
  2487. if (bio_ret && *bio_ret) {
  2488. bio = *bio_ret;
  2489. if (old_compressed)
  2490. contig = bio->bi_iter.bi_sector == sector;
  2491. else
  2492. contig = bio_end_sector(bio) == sector;
  2493. if (prev_bio_flags != bio_flags || !contig ||
  2494. force_bio_submit ||
  2495. merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
  2496. bio_add_page(bio, page, page_size, offset) < page_size) {
  2497. ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
  2498. if (ret < 0) {
  2499. *bio_ret = NULL;
  2500. return ret;
  2501. }
  2502. bio = NULL;
  2503. } else {
  2504. if (wbc)
  2505. wbc_account_io(wbc, page, page_size);
  2506. return 0;
  2507. }
  2508. }
  2509. bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
  2510. GFP_NOFS | __GFP_HIGH);
  2511. if (!bio)
  2512. return -ENOMEM;
  2513. bio_add_page(bio, page, page_size, offset);
  2514. bio->bi_end_io = end_io_func;
  2515. bio->bi_private = tree;
  2516. bio_set_op_attrs(bio, op, op_flags);
  2517. if (wbc) {
  2518. wbc_init_bio(wbc, bio);
  2519. wbc_account_io(wbc, page, page_size);
  2520. }
  2521. if (bio_ret)
  2522. *bio_ret = bio;
  2523. else
  2524. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2525. return ret;
  2526. }
  2527. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2528. struct page *page)
  2529. {
  2530. if (!PagePrivate(page)) {
  2531. SetPagePrivate(page);
  2532. get_page(page);
  2533. set_page_private(page, (unsigned long)eb);
  2534. } else {
  2535. WARN_ON(page->private != (unsigned long)eb);
  2536. }
  2537. }
  2538. void set_page_extent_mapped(struct page *page)
  2539. {
  2540. if (!PagePrivate(page)) {
  2541. SetPagePrivate(page);
  2542. get_page(page);
  2543. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2544. }
  2545. }
  2546. static struct extent_map *
  2547. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2548. u64 start, u64 len, get_extent_t *get_extent,
  2549. struct extent_map **em_cached)
  2550. {
  2551. struct extent_map *em;
  2552. if (em_cached && *em_cached) {
  2553. em = *em_cached;
  2554. if (extent_map_in_tree(em) && start >= em->start &&
  2555. start < extent_map_end(em)) {
  2556. refcount_inc(&em->refs);
  2557. return em;
  2558. }
  2559. free_extent_map(em);
  2560. *em_cached = NULL;
  2561. }
  2562. em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
  2563. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2564. BUG_ON(*em_cached);
  2565. refcount_inc(&em->refs);
  2566. *em_cached = em;
  2567. }
  2568. return em;
  2569. }
  2570. /*
  2571. * basic readpage implementation. Locked extent state structs are inserted
  2572. * into the tree that are removed when the IO is done (by the end_io
  2573. * handlers)
  2574. * XXX JDM: This needs looking at to ensure proper page locking
  2575. * return 0 on success, otherwise return error
  2576. */
  2577. static int __do_readpage(struct extent_io_tree *tree,
  2578. struct page *page,
  2579. get_extent_t *get_extent,
  2580. struct extent_map **em_cached,
  2581. struct bio **bio, int mirror_num,
  2582. unsigned long *bio_flags, int read_flags,
  2583. u64 *prev_em_start)
  2584. {
  2585. struct inode *inode = page->mapping->host;
  2586. u64 start = page_offset(page);
  2587. u64 page_end = start + PAGE_SIZE - 1;
  2588. u64 end;
  2589. u64 cur = start;
  2590. u64 extent_offset;
  2591. u64 last_byte = i_size_read(inode);
  2592. u64 block_start;
  2593. u64 cur_end;
  2594. sector_t sector;
  2595. struct extent_map *em;
  2596. struct block_device *bdev;
  2597. int ret = 0;
  2598. int nr = 0;
  2599. size_t pg_offset = 0;
  2600. size_t iosize;
  2601. size_t disk_io_size;
  2602. size_t blocksize = inode->i_sb->s_blocksize;
  2603. unsigned long this_bio_flag = 0;
  2604. set_page_extent_mapped(page);
  2605. end = page_end;
  2606. if (!PageUptodate(page)) {
  2607. if (cleancache_get_page(page) == 0) {
  2608. BUG_ON(blocksize != PAGE_SIZE);
  2609. unlock_extent(tree, start, end);
  2610. goto out;
  2611. }
  2612. }
  2613. if (page->index == last_byte >> PAGE_SHIFT) {
  2614. char *userpage;
  2615. size_t zero_offset = last_byte & (PAGE_SIZE - 1);
  2616. if (zero_offset) {
  2617. iosize = PAGE_SIZE - zero_offset;
  2618. userpage = kmap_atomic(page);
  2619. memset(userpage + zero_offset, 0, iosize);
  2620. flush_dcache_page(page);
  2621. kunmap_atomic(userpage);
  2622. }
  2623. }
  2624. while (cur <= end) {
  2625. bool force_bio_submit = false;
  2626. if (cur >= last_byte) {
  2627. char *userpage;
  2628. struct extent_state *cached = NULL;
  2629. iosize = PAGE_SIZE - pg_offset;
  2630. userpage = kmap_atomic(page);
  2631. memset(userpage + pg_offset, 0, iosize);
  2632. flush_dcache_page(page);
  2633. kunmap_atomic(userpage);
  2634. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2635. &cached, GFP_NOFS);
  2636. unlock_extent_cached(tree, cur,
  2637. cur + iosize - 1,
  2638. &cached, GFP_NOFS);
  2639. break;
  2640. }
  2641. em = __get_extent_map(inode, page, pg_offset, cur,
  2642. end - cur + 1, get_extent, em_cached);
  2643. if (IS_ERR_OR_NULL(em)) {
  2644. SetPageError(page);
  2645. unlock_extent(tree, cur, end);
  2646. break;
  2647. }
  2648. extent_offset = cur - em->start;
  2649. BUG_ON(extent_map_end(em) <= cur);
  2650. BUG_ON(end < cur);
  2651. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2652. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2653. extent_set_compress_type(&this_bio_flag,
  2654. em->compress_type);
  2655. }
  2656. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2657. cur_end = min(extent_map_end(em) - 1, end);
  2658. iosize = ALIGN(iosize, blocksize);
  2659. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2660. disk_io_size = em->block_len;
  2661. sector = em->block_start >> 9;
  2662. } else {
  2663. sector = (em->block_start + extent_offset) >> 9;
  2664. disk_io_size = iosize;
  2665. }
  2666. bdev = em->bdev;
  2667. block_start = em->block_start;
  2668. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2669. block_start = EXTENT_MAP_HOLE;
  2670. /*
  2671. * If we have a file range that points to a compressed extent
  2672. * and it's followed by a consecutive file range that points to
  2673. * to the same compressed extent (possibly with a different
  2674. * offset and/or length, so it either points to the whole extent
  2675. * or only part of it), we must make sure we do not submit a
  2676. * single bio to populate the pages for the 2 ranges because
  2677. * this makes the compressed extent read zero out the pages
  2678. * belonging to the 2nd range. Imagine the following scenario:
  2679. *
  2680. * File layout
  2681. * [0 - 8K] [8K - 24K]
  2682. * | |
  2683. * | |
  2684. * points to extent X, points to extent X,
  2685. * offset 4K, length of 8K offset 0, length 16K
  2686. *
  2687. * [extent X, compressed length = 4K uncompressed length = 16K]
  2688. *
  2689. * If the bio to read the compressed extent covers both ranges,
  2690. * it will decompress extent X into the pages belonging to the
  2691. * first range and then it will stop, zeroing out the remaining
  2692. * pages that belong to the other range that points to extent X.
  2693. * So here we make sure we submit 2 bios, one for the first
  2694. * range and another one for the third range. Both will target
  2695. * the same physical extent from disk, but we can't currently
  2696. * make the compressed bio endio callback populate the pages
  2697. * for both ranges because each compressed bio is tightly
  2698. * coupled with a single extent map, and each range can have
  2699. * an extent map with a different offset value relative to the
  2700. * uncompressed data of our extent and different lengths. This
  2701. * is a corner case so we prioritize correctness over
  2702. * non-optimal behavior (submitting 2 bios for the same extent).
  2703. */
  2704. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
  2705. prev_em_start && *prev_em_start != (u64)-1 &&
  2706. *prev_em_start != em->orig_start)
  2707. force_bio_submit = true;
  2708. if (prev_em_start)
  2709. *prev_em_start = em->orig_start;
  2710. free_extent_map(em);
  2711. em = NULL;
  2712. /* we've found a hole, just zero and go on */
  2713. if (block_start == EXTENT_MAP_HOLE) {
  2714. char *userpage;
  2715. struct extent_state *cached = NULL;
  2716. userpage = kmap_atomic(page);
  2717. memset(userpage + pg_offset, 0, iosize);
  2718. flush_dcache_page(page);
  2719. kunmap_atomic(userpage);
  2720. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2721. &cached, GFP_NOFS);
  2722. unlock_extent_cached(tree, cur,
  2723. cur + iosize - 1,
  2724. &cached, GFP_NOFS);
  2725. cur = cur + iosize;
  2726. pg_offset += iosize;
  2727. continue;
  2728. }
  2729. /* the get_extent function already copied into the page */
  2730. if (test_range_bit(tree, cur, cur_end,
  2731. EXTENT_UPTODATE, 1, NULL)) {
  2732. check_page_uptodate(tree, page);
  2733. unlock_extent(tree, cur, cur + iosize - 1);
  2734. cur = cur + iosize;
  2735. pg_offset += iosize;
  2736. continue;
  2737. }
  2738. /* we have an inline extent but it didn't get marked up
  2739. * to date. Error out
  2740. */
  2741. if (block_start == EXTENT_MAP_INLINE) {
  2742. SetPageError(page);
  2743. unlock_extent(tree, cur, cur + iosize - 1);
  2744. cur = cur + iosize;
  2745. pg_offset += iosize;
  2746. continue;
  2747. }
  2748. ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL,
  2749. page, sector, disk_io_size, pg_offset,
  2750. bdev, bio,
  2751. end_bio_extent_readpage, mirror_num,
  2752. *bio_flags,
  2753. this_bio_flag,
  2754. force_bio_submit);
  2755. if (!ret) {
  2756. nr++;
  2757. *bio_flags = this_bio_flag;
  2758. } else {
  2759. SetPageError(page);
  2760. unlock_extent(tree, cur, cur + iosize - 1);
  2761. goto out;
  2762. }
  2763. cur = cur + iosize;
  2764. pg_offset += iosize;
  2765. }
  2766. out:
  2767. if (!nr) {
  2768. if (!PageError(page))
  2769. SetPageUptodate(page);
  2770. unlock_page(page);
  2771. }
  2772. return ret;
  2773. }
  2774. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2775. struct page *pages[], int nr_pages,
  2776. u64 start, u64 end,
  2777. get_extent_t *get_extent,
  2778. struct extent_map **em_cached,
  2779. struct bio **bio, int mirror_num,
  2780. unsigned long *bio_flags,
  2781. u64 *prev_em_start)
  2782. {
  2783. struct inode *inode;
  2784. struct btrfs_ordered_extent *ordered;
  2785. int index;
  2786. inode = pages[0]->mapping->host;
  2787. while (1) {
  2788. lock_extent(tree, start, end);
  2789. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2790. end - start + 1);
  2791. if (!ordered)
  2792. break;
  2793. unlock_extent(tree, start, end);
  2794. btrfs_start_ordered_extent(inode, ordered, 1);
  2795. btrfs_put_ordered_extent(ordered);
  2796. }
  2797. for (index = 0; index < nr_pages; index++) {
  2798. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2799. mirror_num, bio_flags, 0, prev_em_start);
  2800. put_page(pages[index]);
  2801. }
  2802. }
  2803. static void __extent_readpages(struct extent_io_tree *tree,
  2804. struct page *pages[],
  2805. int nr_pages, get_extent_t *get_extent,
  2806. struct extent_map **em_cached,
  2807. struct bio **bio, int mirror_num,
  2808. unsigned long *bio_flags,
  2809. u64 *prev_em_start)
  2810. {
  2811. u64 start = 0;
  2812. u64 end = 0;
  2813. u64 page_start;
  2814. int index;
  2815. int first_index = 0;
  2816. for (index = 0; index < nr_pages; index++) {
  2817. page_start = page_offset(pages[index]);
  2818. if (!end) {
  2819. start = page_start;
  2820. end = start + PAGE_SIZE - 1;
  2821. first_index = index;
  2822. } else if (end + 1 == page_start) {
  2823. end += PAGE_SIZE;
  2824. } else {
  2825. __do_contiguous_readpages(tree, &pages[first_index],
  2826. index - first_index, start,
  2827. end, get_extent, em_cached,
  2828. bio, mirror_num, bio_flags,
  2829. prev_em_start);
  2830. start = page_start;
  2831. end = start + PAGE_SIZE - 1;
  2832. first_index = index;
  2833. }
  2834. }
  2835. if (end)
  2836. __do_contiguous_readpages(tree, &pages[first_index],
  2837. index - first_index, start,
  2838. end, get_extent, em_cached, bio,
  2839. mirror_num, bio_flags,
  2840. prev_em_start);
  2841. }
  2842. static int __extent_read_full_page(struct extent_io_tree *tree,
  2843. struct page *page,
  2844. get_extent_t *get_extent,
  2845. struct bio **bio, int mirror_num,
  2846. unsigned long *bio_flags, int read_flags)
  2847. {
  2848. struct inode *inode = page->mapping->host;
  2849. struct btrfs_ordered_extent *ordered;
  2850. u64 start = page_offset(page);
  2851. u64 end = start + PAGE_SIZE - 1;
  2852. int ret;
  2853. while (1) {
  2854. lock_extent(tree, start, end);
  2855. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2856. PAGE_SIZE);
  2857. if (!ordered)
  2858. break;
  2859. unlock_extent(tree, start, end);
  2860. btrfs_start_ordered_extent(inode, ordered, 1);
  2861. btrfs_put_ordered_extent(ordered);
  2862. }
  2863. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2864. bio_flags, read_flags, NULL);
  2865. return ret;
  2866. }
  2867. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2868. get_extent_t *get_extent, int mirror_num)
  2869. {
  2870. struct bio *bio = NULL;
  2871. unsigned long bio_flags = 0;
  2872. int ret;
  2873. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2874. &bio_flags, 0);
  2875. if (bio)
  2876. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2877. return ret;
  2878. }
  2879. static void update_nr_written(struct writeback_control *wbc,
  2880. unsigned long nr_written)
  2881. {
  2882. wbc->nr_to_write -= nr_written;
  2883. }
  2884. /*
  2885. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2886. *
  2887. * This returns 1 if our fill_delalloc function did all the work required
  2888. * to write the page (copy into inline extent). In this case the IO has
  2889. * been started and the page is already unlocked.
  2890. *
  2891. * This returns 0 if all went well (page still locked)
  2892. * This returns < 0 if there were errors (page still locked)
  2893. */
  2894. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2895. struct page *page, struct writeback_control *wbc,
  2896. struct extent_page_data *epd,
  2897. u64 delalloc_start,
  2898. unsigned long *nr_written)
  2899. {
  2900. struct extent_io_tree *tree = epd->tree;
  2901. u64 page_end = delalloc_start + PAGE_SIZE - 1;
  2902. u64 nr_delalloc;
  2903. u64 delalloc_to_write = 0;
  2904. u64 delalloc_end = 0;
  2905. int ret;
  2906. int page_started = 0;
  2907. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2908. return 0;
  2909. while (delalloc_end < page_end) {
  2910. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2911. page,
  2912. &delalloc_start,
  2913. &delalloc_end,
  2914. BTRFS_MAX_EXTENT_SIZE);
  2915. if (nr_delalloc == 0) {
  2916. delalloc_start = delalloc_end + 1;
  2917. continue;
  2918. }
  2919. ret = tree->ops->fill_delalloc(inode, page,
  2920. delalloc_start,
  2921. delalloc_end,
  2922. &page_started,
  2923. nr_written);
  2924. /* File system has been set read-only */
  2925. if (ret) {
  2926. SetPageError(page);
  2927. /* fill_delalloc should be return < 0 for error
  2928. * but just in case, we use > 0 here meaning the
  2929. * IO is started, so we don't want to return > 0
  2930. * unless things are going well.
  2931. */
  2932. ret = ret < 0 ? ret : -EIO;
  2933. goto done;
  2934. }
  2935. /*
  2936. * delalloc_end is already one less than the total length, so
  2937. * we don't subtract one from PAGE_SIZE
  2938. */
  2939. delalloc_to_write += (delalloc_end - delalloc_start +
  2940. PAGE_SIZE) >> PAGE_SHIFT;
  2941. delalloc_start = delalloc_end + 1;
  2942. }
  2943. if (wbc->nr_to_write < delalloc_to_write) {
  2944. int thresh = 8192;
  2945. if (delalloc_to_write < thresh * 2)
  2946. thresh = delalloc_to_write;
  2947. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2948. thresh);
  2949. }
  2950. /* did the fill delalloc function already unlock and start
  2951. * the IO?
  2952. */
  2953. if (page_started) {
  2954. /*
  2955. * we've unlocked the page, so we can't update
  2956. * the mapping's writeback index, just update
  2957. * nr_to_write.
  2958. */
  2959. wbc->nr_to_write -= *nr_written;
  2960. return 1;
  2961. }
  2962. ret = 0;
  2963. done:
  2964. return ret;
  2965. }
  2966. /*
  2967. * helper for __extent_writepage. This calls the writepage start hooks,
  2968. * and does the loop to map the page into extents and bios.
  2969. *
  2970. * We return 1 if the IO is started and the page is unlocked,
  2971. * 0 if all went well (page still locked)
  2972. * < 0 if there were errors (page still locked)
  2973. */
  2974. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2975. struct page *page,
  2976. struct writeback_control *wbc,
  2977. struct extent_page_data *epd,
  2978. loff_t i_size,
  2979. unsigned long nr_written,
  2980. int write_flags, int *nr_ret)
  2981. {
  2982. struct extent_io_tree *tree = epd->tree;
  2983. u64 start = page_offset(page);
  2984. u64 page_end = start + PAGE_SIZE - 1;
  2985. u64 end;
  2986. u64 cur = start;
  2987. u64 extent_offset;
  2988. u64 block_start;
  2989. u64 iosize;
  2990. sector_t sector;
  2991. struct extent_map *em;
  2992. struct block_device *bdev;
  2993. size_t pg_offset = 0;
  2994. size_t blocksize;
  2995. int ret = 0;
  2996. int nr = 0;
  2997. bool compressed;
  2998. if (tree->ops && tree->ops->writepage_start_hook) {
  2999. ret = tree->ops->writepage_start_hook(page, start,
  3000. page_end);
  3001. if (ret) {
  3002. /* Fixup worker will requeue */
  3003. if (ret == -EBUSY)
  3004. wbc->pages_skipped++;
  3005. else
  3006. redirty_page_for_writepage(wbc, page);
  3007. update_nr_written(wbc, nr_written);
  3008. unlock_page(page);
  3009. return 1;
  3010. }
  3011. }
  3012. /*
  3013. * we don't want to touch the inode after unlocking the page,
  3014. * so we update the mapping writeback index now
  3015. */
  3016. update_nr_written(wbc, nr_written + 1);
  3017. end = page_end;
  3018. if (i_size <= start) {
  3019. if (tree->ops && tree->ops->writepage_end_io_hook)
  3020. tree->ops->writepage_end_io_hook(page, start,
  3021. page_end, NULL, 1);
  3022. goto done;
  3023. }
  3024. blocksize = inode->i_sb->s_blocksize;
  3025. while (cur <= end) {
  3026. u64 em_end;
  3027. if (cur >= i_size) {
  3028. if (tree->ops && tree->ops->writepage_end_io_hook)
  3029. tree->ops->writepage_end_io_hook(page, cur,
  3030. page_end, NULL, 1);
  3031. break;
  3032. }
  3033. em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
  3034. end - cur + 1, 1);
  3035. if (IS_ERR_OR_NULL(em)) {
  3036. SetPageError(page);
  3037. ret = PTR_ERR_OR_ZERO(em);
  3038. break;
  3039. }
  3040. extent_offset = cur - em->start;
  3041. em_end = extent_map_end(em);
  3042. BUG_ON(em_end <= cur);
  3043. BUG_ON(end < cur);
  3044. iosize = min(em_end - cur, end - cur + 1);
  3045. iosize = ALIGN(iosize, blocksize);
  3046. sector = (em->block_start + extent_offset) >> 9;
  3047. bdev = em->bdev;
  3048. block_start = em->block_start;
  3049. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3050. free_extent_map(em);
  3051. em = NULL;
  3052. /*
  3053. * compressed and inline extents are written through other
  3054. * paths in the FS
  3055. */
  3056. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3057. block_start == EXTENT_MAP_INLINE) {
  3058. /*
  3059. * end_io notification does not happen here for
  3060. * compressed extents
  3061. */
  3062. if (!compressed && tree->ops &&
  3063. tree->ops->writepage_end_io_hook)
  3064. tree->ops->writepage_end_io_hook(page, cur,
  3065. cur + iosize - 1,
  3066. NULL, 1);
  3067. else if (compressed) {
  3068. /* we don't want to end_page_writeback on
  3069. * a compressed extent. this happens
  3070. * elsewhere
  3071. */
  3072. nr++;
  3073. }
  3074. cur += iosize;
  3075. pg_offset += iosize;
  3076. continue;
  3077. }
  3078. set_range_writeback(tree, cur, cur + iosize - 1);
  3079. if (!PageWriteback(page)) {
  3080. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3081. "page %lu not writeback, cur %llu end %llu",
  3082. page->index, cur, end);
  3083. }
  3084. ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
  3085. page, sector, iosize, pg_offset,
  3086. bdev, &epd->bio,
  3087. end_bio_extent_writepage,
  3088. 0, 0, 0, false);
  3089. if (ret) {
  3090. SetPageError(page);
  3091. if (PageWriteback(page))
  3092. end_page_writeback(page);
  3093. }
  3094. cur = cur + iosize;
  3095. pg_offset += iosize;
  3096. nr++;
  3097. }
  3098. done:
  3099. *nr_ret = nr;
  3100. return ret;
  3101. }
  3102. /*
  3103. * the writepage semantics are similar to regular writepage. extent
  3104. * records are inserted to lock ranges in the tree, and as dirty areas
  3105. * are found, they are marked writeback. Then the lock bits are removed
  3106. * and the end_io handler clears the writeback ranges
  3107. */
  3108. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3109. void *data)
  3110. {
  3111. struct inode *inode = page->mapping->host;
  3112. struct extent_page_data *epd = data;
  3113. u64 start = page_offset(page);
  3114. u64 page_end = start + PAGE_SIZE - 1;
  3115. int ret;
  3116. int nr = 0;
  3117. size_t pg_offset = 0;
  3118. loff_t i_size = i_size_read(inode);
  3119. unsigned long end_index = i_size >> PAGE_SHIFT;
  3120. int write_flags = 0;
  3121. unsigned long nr_written = 0;
  3122. if (wbc->sync_mode == WB_SYNC_ALL)
  3123. write_flags = REQ_SYNC;
  3124. trace___extent_writepage(page, inode, wbc);
  3125. WARN_ON(!PageLocked(page));
  3126. ClearPageError(page);
  3127. pg_offset = i_size & (PAGE_SIZE - 1);
  3128. if (page->index > end_index ||
  3129. (page->index == end_index && !pg_offset)) {
  3130. page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
  3131. unlock_page(page);
  3132. return 0;
  3133. }
  3134. if (page->index == end_index) {
  3135. char *userpage;
  3136. userpage = kmap_atomic(page);
  3137. memset(userpage + pg_offset, 0,
  3138. PAGE_SIZE - pg_offset);
  3139. kunmap_atomic(userpage);
  3140. flush_dcache_page(page);
  3141. }
  3142. pg_offset = 0;
  3143. set_page_extent_mapped(page);
  3144. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3145. if (ret == 1)
  3146. goto done_unlocked;
  3147. if (ret)
  3148. goto done;
  3149. ret = __extent_writepage_io(inode, page, wbc, epd,
  3150. i_size, nr_written, write_flags, &nr);
  3151. if (ret == 1)
  3152. goto done_unlocked;
  3153. done:
  3154. if (nr == 0) {
  3155. /* make sure the mapping tag for page dirty gets cleared */
  3156. set_page_writeback(page);
  3157. end_page_writeback(page);
  3158. }
  3159. if (PageError(page)) {
  3160. ret = ret < 0 ? ret : -EIO;
  3161. end_extent_writepage(page, ret, start, page_end);
  3162. }
  3163. unlock_page(page);
  3164. return ret;
  3165. done_unlocked:
  3166. return 0;
  3167. }
  3168. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3169. {
  3170. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3171. TASK_UNINTERRUPTIBLE);
  3172. }
  3173. static noinline_for_stack int
  3174. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3175. struct btrfs_fs_info *fs_info,
  3176. struct extent_page_data *epd)
  3177. {
  3178. unsigned long i, num_pages;
  3179. int flush = 0;
  3180. int ret = 0;
  3181. if (!btrfs_try_tree_write_lock(eb)) {
  3182. flush = 1;
  3183. flush_write_bio(epd);
  3184. btrfs_tree_lock(eb);
  3185. }
  3186. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3187. btrfs_tree_unlock(eb);
  3188. if (!epd->sync_io)
  3189. return 0;
  3190. if (!flush) {
  3191. flush_write_bio(epd);
  3192. flush = 1;
  3193. }
  3194. while (1) {
  3195. wait_on_extent_buffer_writeback(eb);
  3196. btrfs_tree_lock(eb);
  3197. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3198. break;
  3199. btrfs_tree_unlock(eb);
  3200. }
  3201. }
  3202. /*
  3203. * We need to do this to prevent races in people who check if the eb is
  3204. * under IO since we can end up having no IO bits set for a short period
  3205. * of time.
  3206. */
  3207. spin_lock(&eb->refs_lock);
  3208. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3209. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3210. spin_unlock(&eb->refs_lock);
  3211. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3212. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3213. -eb->len,
  3214. fs_info->dirty_metadata_batch);
  3215. ret = 1;
  3216. } else {
  3217. spin_unlock(&eb->refs_lock);
  3218. }
  3219. btrfs_tree_unlock(eb);
  3220. if (!ret)
  3221. return ret;
  3222. num_pages = num_extent_pages(eb->start, eb->len);
  3223. for (i = 0; i < num_pages; i++) {
  3224. struct page *p = eb->pages[i];
  3225. if (!trylock_page(p)) {
  3226. if (!flush) {
  3227. flush_write_bio(epd);
  3228. flush = 1;
  3229. }
  3230. lock_page(p);
  3231. }
  3232. }
  3233. return ret;
  3234. }
  3235. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3236. {
  3237. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3238. smp_mb__after_atomic();
  3239. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3240. }
  3241. static void set_btree_ioerr(struct page *page)
  3242. {
  3243. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3244. SetPageError(page);
  3245. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3246. return;
  3247. /*
  3248. * If writeback for a btree extent that doesn't belong to a log tree
  3249. * failed, increment the counter transaction->eb_write_errors.
  3250. * We do this because while the transaction is running and before it's
  3251. * committing (when we call filemap_fdata[write|wait]_range against
  3252. * the btree inode), we might have
  3253. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3254. * returns an error or an error happens during writeback, when we're
  3255. * committing the transaction we wouldn't know about it, since the pages
  3256. * can be no longer dirty nor marked anymore for writeback (if a
  3257. * subsequent modification to the extent buffer didn't happen before the
  3258. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3259. * able to find the pages tagged with SetPageError at transaction
  3260. * commit time. So if this happens we must abort the transaction,
  3261. * otherwise we commit a super block with btree roots that point to
  3262. * btree nodes/leafs whose content on disk is invalid - either garbage
  3263. * or the content of some node/leaf from a past generation that got
  3264. * cowed or deleted and is no longer valid.
  3265. *
  3266. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3267. * not be enough - we need to distinguish between log tree extents vs
  3268. * non-log tree extents, and the next filemap_fdatawait_range() call
  3269. * will catch and clear such errors in the mapping - and that call might
  3270. * be from a log sync and not from a transaction commit. Also, checking
  3271. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3272. * not done and would not be reliable - the eb might have been released
  3273. * from memory and reading it back again means that flag would not be
  3274. * set (since it's a runtime flag, not persisted on disk).
  3275. *
  3276. * Using the flags below in the btree inode also makes us achieve the
  3277. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3278. * writeback for all dirty pages and before filemap_fdatawait_range()
  3279. * is called, the writeback for all dirty pages had already finished
  3280. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3281. * filemap_fdatawait_range() would return success, as it could not know
  3282. * that writeback errors happened (the pages were no longer tagged for
  3283. * writeback).
  3284. */
  3285. switch (eb->log_index) {
  3286. case -1:
  3287. set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
  3288. break;
  3289. case 0:
  3290. set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
  3291. break;
  3292. case 1:
  3293. set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
  3294. break;
  3295. default:
  3296. BUG(); /* unexpected, logic error */
  3297. }
  3298. }
  3299. static void end_bio_extent_buffer_writepage(struct bio *bio)
  3300. {
  3301. struct bio_vec *bvec;
  3302. struct extent_buffer *eb;
  3303. int i, done;
  3304. bio_for_each_segment_all(bvec, bio, i) {
  3305. struct page *page = bvec->bv_page;
  3306. eb = (struct extent_buffer *)page->private;
  3307. BUG_ON(!eb);
  3308. done = atomic_dec_and_test(&eb->io_pages);
  3309. if (bio->bi_status ||
  3310. test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3311. ClearPageUptodate(page);
  3312. set_btree_ioerr(page);
  3313. }
  3314. end_page_writeback(page);
  3315. if (!done)
  3316. continue;
  3317. end_extent_buffer_writeback(eb);
  3318. }
  3319. bio_put(bio);
  3320. }
  3321. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3322. struct btrfs_fs_info *fs_info,
  3323. struct writeback_control *wbc,
  3324. struct extent_page_data *epd)
  3325. {
  3326. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3327. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3328. u64 offset = eb->start;
  3329. u32 nritems;
  3330. unsigned long i, num_pages;
  3331. unsigned long bio_flags = 0;
  3332. unsigned long start, end;
  3333. int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META;
  3334. int ret = 0;
  3335. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3336. num_pages = num_extent_pages(eb->start, eb->len);
  3337. atomic_set(&eb->io_pages, num_pages);
  3338. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3339. bio_flags = EXTENT_BIO_TREE_LOG;
  3340. /* set btree blocks beyond nritems with 0 to avoid stale content. */
  3341. nritems = btrfs_header_nritems(eb);
  3342. if (btrfs_header_level(eb) > 0) {
  3343. end = btrfs_node_key_ptr_offset(nritems);
  3344. memzero_extent_buffer(eb, end, eb->len - end);
  3345. } else {
  3346. /*
  3347. * leaf:
  3348. * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
  3349. */
  3350. start = btrfs_item_nr_offset(nritems);
  3351. end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb);
  3352. memzero_extent_buffer(eb, start, end - start);
  3353. }
  3354. for (i = 0; i < num_pages; i++) {
  3355. struct page *p = eb->pages[i];
  3356. clear_page_dirty_for_io(p);
  3357. set_page_writeback(p);
  3358. ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
  3359. p, offset >> 9, PAGE_SIZE, 0, bdev,
  3360. &epd->bio,
  3361. end_bio_extent_buffer_writepage,
  3362. 0, epd->bio_flags, bio_flags, false);
  3363. epd->bio_flags = bio_flags;
  3364. if (ret) {
  3365. set_btree_ioerr(p);
  3366. if (PageWriteback(p))
  3367. end_page_writeback(p);
  3368. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3369. end_extent_buffer_writeback(eb);
  3370. ret = -EIO;
  3371. break;
  3372. }
  3373. offset += PAGE_SIZE;
  3374. update_nr_written(wbc, 1);
  3375. unlock_page(p);
  3376. }
  3377. if (unlikely(ret)) {
  3378. for (; i < num_pages; i++) {
  3379. struct page *p = eb->pages[i];
  3380. clear_page_dirty_for_io(p);
  3381. unlock_page(p);
  3382. }
  3383. }
  3384. return ret;
  3385. }
  3386. int btree_write_cache_pages(struct address_space *mapping,
  3387. struct writeback_control *wbc)
  3388. {
  3389. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3390. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3391. struct extent_buffer *eb, *prev_eb = NULL;
  3392. struct extent_page_data epd = {
  3393. .bio = NULL,
  3394. .tree = tree,
  3395. .extent_locked = 0,
  3396. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3397. .bio_flags = 0,
  3398. };
  3399. int ret = 0;
  3400. int done = 0;
  3401. int nr_to_write_done = 0;
  3402. struct pagevec pvec;
  3403. int nr_pages;
  3404. pgoff_t index;
  3405. pgoff_t end; /* Inclusive */
  3406. int scanned = 0;
  3407. int tag;
  3408. pagevec_init(&pvec, 0);
  3409. if (wbc->range_cyclic) {
  3410. index = mapping->writeback_index; /* Start from prev offset */
  3411. end = -1;
  3412. } else {
  3413. index = wbc->range_start >> PAGE_SHIFT;
  3414. end = wbc->range_end >> PAGE_SHIFT;
  3415. scanned = 1;
  3416. }
  3417. if (wbc->sync_mode == WB_SYNC_ALL)
  3418. tag = PAGECACHE_TAG_TOWRITE;
  3419. else
  3420. tag = PAGECACHE_TAG_DIRTY;
  3421. retry:
  3422. if (wbc->sync_mode == WB_SYNC_ALL)
  3423. tag_pages_for_writeback(mapping, index, end);
  3424. while (!done && !nr_to_write_done && (index <= end) &&
  3425. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3426. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3427. unsigned i;
  3428. scanned = 1;
  3429. for (i = 0; i < nr_pages; i++) {
  3430. struct page *page = pvec.pages[i];
  3431. if (!PagePrivate(page))
  3432. continue;
  3433. if (!wbc->range_cyclic && page->index > end) {
  3434. done = 1;
  3435. break;
  3436. }
  3437. spin_lock(&mapping->private_lock);
  3438. if (!PagePrivate(page)) {
  3439. spin_unlock(&mapping->private_lock);
  3440. continue;
  3441. }
  3442. eb = (struct extent_buffer *)page->private;
  3443. /*
  3444. * Shouldn't happen and normally this would be a BUG_ON
  3445. * but no sense in crashing the users box for something
  3446. * we can survive anyway.
  3447. */
  3448. if (WARN_ON(!eb)) {
  3449. spin_unlock(&mapping->private_lock);
  3450. continue;
  3451. }
  3452. if (eb == prev_eb) {
  3453. spin_unlock(&mapping->private_lock);
  3454. continue;
  3455. }
  3456. ret = atomic_inc_not_zero(&eb->refs);
  3457. spin_unlock(&mapping->private_lock);
  3458. if (!ret)
  3459. continue;
  3460. prev_eb = eb;
  3461. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3462. if (!ret) {
  3463. free_extent_buffer(eb);
  3464. continue;
  3465. }
  3466. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3467. if (ret) {
  3468. done = 1;
  3469. free_extent_buffer(eb);
  3470. break;
  3471. }
  3472. free_extent_buffer(eb);
  3473. /*
  3474. * the filesystem may choose to bump up nr_to_write.
  3475. * We have to make sure to honor the new nr_to_write
  3476. * at any time
  3477. */
  3478. nr_to_write_done = wbc->nr_to_write <= 0;
  3479. }
  3480. pagevec_release(&pvec);
  3481. cond_resched();
  3482. }
  3483. if (!scanned && !done) {
  3484. /*
  3485. * We hit the last page and there is more work to be done: wrap
  3486. * back to the start of the file
  3487. */
  3488. scanned = 1;
  3489. index = 0;
  3490. goto retry;
  3491. }
  3492. flush_write_bio(&epd);
  3493. return ret;
  3494. }
  3495. /**
  3496. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3497. * @mapping: address space structure to write
  3498. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3499. * @writepage: function called for each page
  3500. * @data: data passed to writepage function
  3501. *
  3502. * If a page is already under I/O, write_cache_pages() skips it, even
  3503. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3504. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3505. * and msync() need to guarantee that all the data which was dirty at the time
  3506. * the call was made get new I/O started against them. If wbc->sync_mode is
  3507. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3508. * existing IO to complete.
  3509. */
  3510. static int extent_write_cache_pages(struct address_space *mapping,
  3511. struct writeback_control *wbc,
  3512. writepage_t writepage, void *data,
  3513. void (*flush_fn)(void *))
  3514. {
  3515. struct inode *inode = mapping->host;
  3516. int ret = 0;
  3517. int done = 0;
  3518. int nr_to_write_done = 0;
  3519. struct pagevec pvec;
  3520. int nr_pages;
  3521. pgoff_t index;
  3522. pgoff_t end; /* Inclusive */
  3523. pgoff_t done_index;
  3524. int range_whole = 0;
  3525. int scanned = 0;
  3526. int tag;
  3527. /*
  3528. * We have to hold onto the inode so that ordered extents can do their
  3529. * work when the IO finishes. The alternative to this is failing to add
  3530. * an ordered extent if the igrab() fails there and that is a huge pain
  3531. * to deal with, so instead just hold onto the inode throughout the
  3532. * writepages operation. If it fails here we are freeing up the inode
  3533. * anyway and we'd rather not waste our time writing out stuff that is
  3534. * going to be truncated anyway.
  3535. */
  3536. if (!igrab(inode))
  3537. return 0;
  3538. pagevec_init(&pvec, 0);
  3539. if (wbc->range_cyclic) {
  3540. index = mapping->writeback_index; /* Start from prev offset */
  3541. end = -1;
  3542. } else {
  3543. index = wbc->range_start >> PAGE_SHIFT;
  3544. end = wbc->range_end >> PAGE_SHIFT;
  3545. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  3546. range_whole = 1;
  3547. scanned = 1;
  3548. }
  3549. if (wbc->sync_mode == WB_SYNC_ALL)
  3550. tag = PAGECACHE_TAG_TOWRITE;
  3551. else
  3552. tag = PAGECACHE_TAG_DIRTY;
  3553. retry:
  3554. if (wbc->sync_mode == WB_SYNC_ALL)
  3555. tag_pages_for_writeback(mapping, index, end);
  3556. done_index = index;
  3557. while (!done && !nr_to_write_done && (index <= end) &&
  3558. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3559. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3560. unsigned i;
  3561. scanned = 1;
  3562. for (i = 0; i < nr_pages; i++) {
  3563. struct page *page = pvec.pages[i];
  3564. done_index = page->index;
  3565. /*
  3566. * At this point we hold neither mapping->tree_lock nor
  3567. * lock on the page itself: the page may be truncated or
  3568. * invalidated (changing page->mapping to NULL), or even
  3569. * swizzled back from swapper_space to tmpfs file
  3570. * mapping
  3571. */
  3572. if (!trylock_page(page)) {
  3573. flush_fn(data);
  3574. lock_page(page);
  3575. }
  3576. if (unlikely(page->mapping != mapping)) {
  3577. unlock_page(page);
  3578. continue;
  3579. }
  3580. if (!wbc->range_cyclic && page->index > end) {
  3581. done = 1;
  3582. unlock_page(page);
  3583. continue;
  3584. }
  3585. if (wbc->sync_mode != WB_SYNC_NONE) {
  3586. if (PageWriteback(page))
  3587. flush_fn(data);
  3588. wait_on_page_writeback(page);
  3589. }
  3590. if (PageWriteback(page) ||
  3591. !clear_page_dirty_for_io(page)) {
  3592. unlock_page(page);
  3593. continue;
  3594. }
  3595. ret = (*writepage)(page, wbc, data);
  3596. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3597. unlock_page(page);
  3598. ret = 0;
  3599. }
  3600. if (ret < 0) {
  3601. /*
  3602. * done_index is set past this page,
  3603. * so media errors will not choke
  3604. * background writeout for the entire
  3605. * file. This has consequences for
  3606. * range_cyclic semantics (ie. it may
  3607. * not be suitable for data integrity
  3608. * writeout).
  3609. */
  3610. done_index = page->index + 1;
  3611. done = 1;
  3612. break;
  3613. }
  3614. /*
  3615. * the filesystem may choose to bump up nr_to_write.
  3616. * We have to make sure to honor the new nr_to_write
  3617. * at any time
  3618. */
  3619. nr_to_write_done = wbc->nr_to_write <= 0;
  3620. }
  3621. pagevec_release(&pvec);
  3622. cond_resched();
  3623. }
  3624. if (!scanned && !done) {
  3625. /*
  3626. * We hit the last page and there is more work to be done: wrap
  3627. * back to the start of the file
  3628. */
  3629. scanned = 1;
  3630. index = 0;
  3631. goto retry;
  3632. }
  3633. if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
  3634. mapping->writeback_index = done_index;
  3635. btrfs_add_delayed_iput(inode);
  3636. return ret;
  3637. }
  3638. static void flush_epd_write_bio(struct extent_page_data *epd)
  3639. {
  3640. if (epd->bio) {
  3641. int ret;
  3642. bio_set_op_attrs(epd->bio, REQ_OP_WRITE,
  3643. epd->sync_io ? REQ_SYNC : 0);
  3644. ret = submit_one_bio(epd->bio, 0, epd->bio_flags);
  3645. BUG_ON(ret < 0); /* -ENOMEM */
  3646. epd->bio = NULL;
  3647. }
  3648. }
  3649. static noinline void flush_write_bio(void *data)
  3650. {
  3651. struct extent_page_data *epd = data;
  3652. flush_epd_write_bio(epd);
  3653. }
  3654. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3655. get_extent_t *get_extent,
  3656. struct writeback_control *wbc)
  3657. {
  3658. int ret;
  3659. struct extent_page_data epd = {
  3660. .bio = NULL,
  3661. .tree = tree,
  3662. .get_extent = get_extent,
  3663. .extent_locked = 0,
  3664. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3665. .bio_flags = 0,
  3666. };
  3667. ret = __extent_writepage(page, wbc, &epd);
  3668. flush_epd_write_bio(&epd);
  3669. return ret;
  3670. }
  3671. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3672. u64 start, u64 end, get_extent_t *get_extent,
  3673. int mode)
  3674. {
  3675. int ret = 0;
  3676. struct address_space *mapping = inode->i_mapping;
  3677. struct page *page;
  3678. unsigned long nr_pages = (end - start + PAGE_SIZE) >>
  3679. PAGE_SHIFT;
  3680. struct extent_page_data epd = {
  3681. .bio = NULL,
  3682. .tree = tree,
  3683. .get_extent = get_extent,
  3684. .extent_locked = 1,
  3685. .sync_io = mode == WB_SYNC_ALL,
  3686. .bio_flags = 0,
  3687. };
  3688. struct writeback_control wbc_writepages = {
  3689. .sync_mode = mode,
  3690. .nr_to_write = nr_pages * 2,
  3691. .range_start = start,
  3692. .range_end = end + 1,
  3693. };
  3694. while (start <= end) {
  3695. page = find_get_page(mapping, start >> PAGE_SHIFT);
  3696. if (clear_page_dirty_for_io(page))
  3697. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3698. else {
  3699. if (tree->ops && tree->ops->writepage_end_io_hook)
  3700. tree->ops->writepage_end_io_hook(page, start,
  3701. start + PAGE_SIZE - 1,
  3702. NULL, 1);
  3703. unlock_page(page);
  3704. }
  3705. put_page(page);
  3706. start += PAGE_SIZE;
  3707. }
  3708. flush_epd_write_bio(&epd);
  3709. return ret;
  3710. }
  3711. int extent_writepages(struct extent_io_tree *tree,
  3712. struct address_space *mapping,
  3713. get_extent_t *get_extent,
  3714. struct writeback_control *wbc)
  3715. {
  3716. int ret = 0;
  3717. struct extent_page_data epd = {
  3718. .bio = NULL,
  3719. .tree = tree,
  3720. .get_extent = get_extent,
  3721. .extent_locked = 0,
  3722. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3723. .bio_flags = 0,
  3724. };
  3725. ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
  3726. flush_write_bio);
  3727. flush_epd_write_bio(&epd);
  3728. return ret;
  3729. }
  3730. int extent_readpages(struct extent_io_tree *tree,
  3731. struct address_space *mapping,
  3732. struct list_head *pages, unsigned nr_pages,
  3733. get_extent_t get_extent)
  3734. {
  3735. struct bio *bio = NULL;
  3736. unsigned page_idx;
  3737. unsigned long bio_flags = 0;
  3738. struct page *pagepool[16];
  3739. struct page *page;
  3740. struct extent_map *em_cached = NULL;
  3741. int nr = 0;
  3742. u64 prev_em_start = (u64)-1;
  3743. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3744. page = list_entry(pages->prev, struct page, lru);
  3745. prefetchw(&page->flags);
  3746. list_del(&page->lru);
  3747. if (add_to_page_cache_lru(page, mapping,
  3748. page->index,
  3749. readahead_gfp_mask(mapping))) {
  3750. put_page(page);
  3751. continue;
  3752. }
  3753. pagepool[nr++] = page;
  3754. if (nr < ARRAY_SIZE(pagepool))
  3755. continue;
  3756. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3757. &bio, 0, &bio_flags, &prev_em_start);
  3758. nr = 0;
  3759. }
  3760. if (nr)
  3761. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3762. &bio, 0, &bio_flags, &prev_em_start);
  3763. if (em_cached)
  3764. free_extent_map(em_cached);
  3765. BUG_ON(!list_empty(pages));
  3766. if (bio)
  3767. return submit_one_bio(bio, 0, bio_flags);
  3768. return 0;
  3769. }
  3770. /*
  3771. * basic invalidatepage code, this waits on any locked or writeback
  3772. * ranges corresponding to the page, and then deletes any extent state
  3773. * records from the tree
  3774. */
  3775. int extent_invalidatepage(struct extent_io_tree *tree,
  3776. struct page *page, unsigned long offset)
  3777. {
  3778. struct extent_state *cached_state = NULL;
  3779. u64 start = page_offset(page);
  3780. u64 end = start + PAGE_SIZE - 1;
  3781. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3782. start += ALIGN(offset, blocksize);
  3783. if (start > end)
  3784. return 0;
  3785. lock_extent_bits(tree, start, end, &cached_state);
  3786. wait_on_page_writeback(page);
  3787. clear_extent_bit(tree, start, end,
  3788. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3789. EXTENT_DO_ACCOUNTING,
  3790. 1, 1, &cached_state, GFP_NOFS);
  3791. return 0;
  3792. }
  3793. /*
  3794. * a helper for releasepage, this tests for areas of the page that
  3795. * are locked or under IO and drops the related state bits if it is safe
  3796. * to drop the page.
  3797. */
  3798. static int try_release_extent_state(struct extent_map_tree *map,
  3799. struct extent_io_tree *tree,
  3800. struct page *page, gfp_t mask)
  3801. {
  3802. u64 start = page_offset(page);
  3803. u64 end = start + PAGE_SIZE - 1;
  3804. int ret = 1;
  3805. if (test_range_bit(tree, start, end,
  3806. EXTENT_IOBITS, 0, NULL))
  3807. ret = 0;
  3808. else {
  3809. /*
  3810. * at this point we can safely clear everything except the
  3811. * locked bit and the nodatasum bit
  3812. */
  3813. ret = clear_extent_bit(tree, start, end,
  3814. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3815. 0, 0, NULL, mask);
  3816. /* if clear_extent_bit failed for enomem reasons,
  3817. * we can't allow the release to continue.
  3818. */
  3819. if (ret < 0)
  3820. ret = 0;
  3821. else
  3822. ret = 1;
  3823. }
  3824. return ret;
  3825. }
  3826. /*
  3827. * a helper for releasepage. As long as there are no locked extents
  3828. * in the range corresponding to the page, both state records and extent
  3829. * map records are removed
  3830. */
  3831. int try_release_extent_mapping(struct extent_map_tree *map,
  3832. struct extent_io_tree *tree, struct page *page,
  3833. gfp_t mask)
  3834. {
  3835. struct extent_map *em;
  3836. u64 start = page_offset(page);
  3837. u64 end = start + PAGE_SIZE - 1;
  3838. if (gfpflags_allow_blocking(mask) &&
  3839. page->mapping->host->i_size > SZ_16M) {
  3840. u64 len;
  3841. while (start <= end) {
  3842. len = end - start + 1;
  3843. write_lock(&map->lock);
  3844. em = lookup_extent_mapping(map, start, len);
  3845. if (!em) {
  3846. write_unlock(&map->lock);
  3847. break;
  3848. }
  3849. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3850. em->start != start) {
  3851. write_unlock(&map->lock);
  3852. free_extent_map(em);
  3853. break;
  3854. }
  3855. if (!test_range_bit(tree, em->start,
  3856. extent_map_end(em) - 1,
  3857. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3858. 0, NULL)) {
  3859. remove_extent_mapping(map, em);
  3860. /* once for the rb tree */
  3861. free_extent_map(em);
  3862. }
  3863. start = extent_map_end(em);
  3864. write_unlock(&map->lock);
  3865. /* once for us */
  3866. free_extent_map(em);
  3867. }
  3868. }
  3869. return try_release_extent_state(map, tree, page, mask);
  3870. }
  3871. /*
  3872. * helper function for fiemap, which doesn't want to see any holes.
  3873. * This maps until we find something past 'last'
  3874. */
  3875. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3876. u64 offset,
  3877. u64 last,
  3878. get_extent_t *get_extent)
  3879. {
  3880. u64 sectorsize = btrfs_inode_sectorsize(inode);
  3881. struct extent_map *em;
  3882. u64 len;
  3883. if (offset >= last)
  3884. return NULL;
  3885. while (1) {
  3886. len = last - offset;
  3887. if (len == 0)
  3888. break;
  3889. len = ALIGN(len, sectorsize);
  3890. em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
  3891. if (IS_ERR_OR_NULL(em))
  3892. return em;
  3893. /* if this isn't a hole return it */
  3894. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3895. em->block_start != EXTENT_MAP_HOLE) {
  3896. return em;
  3897. }
  3898. /* this is a hole, advance to the next extent */
  3899. offset = extent_map_end(em);
  3900. free_extent_map(em);
  3901. if (offset >= last)
  3902. break;
  3903. }
  3904. return NULL;
  3905. }
  3906. /*
  3907. * To cache previous fiemap extent
  3908. *
  3909. * Will be used for merging fiemap extent
  3910. */
  3911. struct fiemap_cache {
  3912. u64 offset;
  3913. u64 phys;
  3914. u64 len;
  3915. u32 flags;
  3916. bool cached;
  3917. };
  3918. /*
  3919. * Helper to submit fiemap extent.
  3920. *
  3921. * Will try to merge current fiemap extent specified by @offset, @phys,
  3922. * @len and @flags with cached one.
  3923. * And only when we fails to merge, cached one will be submitted as
  3924. * fiemap extent.
  3925. *
  3926. * Return value is the same as fiemap_fill_next_extent().
  3927. */
  3928. static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
  3929. struct fiemap_cache *cache,
  3930. u64 offset, u64 phys, u64 len, u32 flags)
  3931. {
  3932. int ret = 0;
  3933. if (!cache->cached)
  3934. goto assign;
  3935. /*
  3936. * Sanity check, extent_fiemap() should have ensured that new
  3937. * fiemap extent won't overlap with cahced one.
  3938. * Not recoverable.
  3939. *
  3940. * NOTE: Physical address can overlap, due to compression
  3941. */
  3942. if (cache->offset + cache->len > offset) {
  3943. WARN_ON(1);
  3944. return -EINVAL;
  3945. }
  3946. /*
  3947. * Only merges fiemap extents if
  3948. * 1) Their logical addresses are continuous
  3949. *
  3950. * 2) Their physical addresses are continuous
  3951. * So truly compressed (physical size smaller than logical size)
  3952. * extents won't get merged with each other
  3953. *
  3954. * 3) Share same flags except FIEMAP_EXTENT_LAST
  3955. * So regular extent won't get merged with prealloc extent
  3956. */
  3957. if (cache->offset + cache->len == offset &&
  3958. cache->phys + cache->len == phys &&
  3959. (cache->flags & ~FIEMAP_EXTENT_LAST) ==
  3960. (flags & ~FIEMAP_EXTENT_LAST)) {
  3961. cache->len += len;
  3962. cache->flags |= flags;
  3963. goto try_submit_last;
  3964. }
  3965. /* Not mergeable, need to submit cached one */
  3966. ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
  3967. cache->len, cache->flags);
  3968. cache->cached = false;
  3969. if (ret)
  3970. return ret;
  3971. assign:
  3972. cache->cached = true;
  3973. cache->offset = offset;
  3974. cache->phys = phys;
  3975. cache->len = len;
  3976. cache->flags = flags;
  3977. try_submit_last:
  3978. if (cache->flags & FIEMAP_EXTENT_LAST) {
  3979. ret = fiemap_fill_next_extent(fieinfo, cache->offset,
  3980. cache->phys, cache->len, cache->flags);
  3981. cache->cached = false;
  3982. }
  3983. return ret;
  3984. }
  3985. /*
  3986. * Sanity check for fiemap cache
  3987. *
  3988. * All fiemap cache should be submitted by emit_fiemap_extent()
  3989. * Iteration should be terminated either by last fiemap extent or
  3990. * fieinfo->fi_extents_max.
  3991. * So no cached fiemap should exist.
  3992. */
  3993. static int check_fiemap_cache(struct btrfs_fs_info *fs_info,
  3994. struct fiemap_extent_info *fieinfo,
  3995. struct fiemap_cache *cache)
  3996. {
  3997. int ret;
  3998. if (!cache->cached)
  3999. return 0;
  4000. /* Small and recoverbale problem, only to info developer */
  4001. #ifdef CONFIG_BTRFS_DEBUG
  4002. WARN_ON(1);
  4003. #endif
  4004. btrfs_warn(fs_info,
  4005. "unhandled fiemap cache detected: offset=%llu phys=%llu len=%llu flags=0x%x",
  4006. cache->offset, cache->phys, cache->len, cache->flags);
  4007. ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
  4008. cache->len, cache->flags);
  4009. cache->cached = false;
  4010. if (ret > 0)
  4011. ret = 0;
  4012. return ret;
  4013. }
  4014. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4015. __u64 start, __u64 len, get_extent_t *get_extent)
  4016. {
  4017. int ret = 0;
  4018. u64 off = start;
  4019. u64 max = start + len;
  4020. u32 flags = 0;
  4021. u32 found_type;
  4022. u64 last;
  4023. u64 last_for_get_extent = 0;
  4024. u64 disko = 0;
  4025. u64 isize = i_size_read(inode);
  4026. struct btrfs_key found_key;
  4027. struct extent_map *em = NULL;
  4028. struct extent_state *cached_state = NULL;
  4029. struct btrfs_path *path;
  4030. struct btrfs_root *root = BTRFS_I(inode)->root;
  4031. struct fiemap_cache cache = { 0 };
  4032. int end = 0;
  4033. u64 em_start = 0;
  4034. u64 em_len = 0;
  4035. u64 em_end = 0;
  4036. if (len == 0)
  4037. return -EINVAL;
  4038. path = btrfs_alloc_path();
  4039. if (!path)
  4040. return -ENOMEM;
  4041. path->leave_spinning = 1;
  4042. start = round_down(start, btrfs_inode_sectorsize(inode));
  4043. len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
  4044. /*
  4045. * lookup the last file extent. We're not using i_size here
  4046. * because there might be preallocation past i_size
  4047. */
  4048. ret = btrfs_lookup_file_extent(NULL, root, path,
  4049. btrfs_ino(BTRFS_I(inode)), -1, 0);
  4050. if (ret < 0) {
  4051. btrfs_free_path(path);
  4052. return ret;
  4053. } else {
  4054. WARN_ON(!ret);
  4055. if (ret == 1)
  4056. ret = 0;
  4057. }
  4058. path->slots[0]--;
  4059. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  4060. found_type = found_key.type;
  4061. /* No extents, but there might be delalloc bits */
  4062. if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
  4063. found_type != BTRFS_EXTENT_DATA_KEY) {
  4064. /* have to trust i_size as the end */
  4065. last = (u64)-1;
  4066. last_for_get_extent = isize;
  4067. } else {
  4068. /*
  4069. * remember the start of the last extent. There are a
  4070. * bunch of different factors that go into the length of the
  4071. * extent, so its much less complex to remember where it started
  4072. */
  4073. last = found_key.offset;
  4074. last_for_get_extent = last + 1;
  4075. }
  4076. btrfs_release_path(path);
  4077. /*
  4078. * we might have some extents allocated but more delalloc past those
  4079. * extents. so, we trust isize unless the start of the last extent is
  4080. * beyond isize
  4081. */
  4082. if (last < isize) {
  4083. last = (u64)-1;
  4084. last_for_get_extent = isize;
  4085. }
  4086. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4087. &cached_state);
  4088. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  4089. get_extent);
  4090. if (!em)
  4091. goto out;
  4092. if (IS_ERR(em)) {
  4093. ret = PTR_ERR(em);
  4094. goto out;
  4095. }
  4096. while (!end) {
  4097. u64 offset_in_extent = 0;
  4098. /* break if the extent we found is outside the range */
  4099. if (em->start >= max || extent_map_end(em) < off)
  4100. break;
  4101. /*
  4102. * get_extent may return an extent that starts before our
  4103. * requested range. We have to make sure the ranges
  4104. * we return to fiemap always move forward and don't
  4105. * overlap, so adjust the offsets here
  4106. */
  4107. em_start = max(em->start, off);
  4108. /*
  4109. * record the offset from the start of the extent
  4110. * for adjusting the disk offset below. Only do this if the
  4111. * extent isn't compressed since our in ram offset may be past
  4112. * what we have actually allocated on disk.
  4113. */
  4114. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4115. offset_in_extent = em_start - em->start;
  4116. em_end = extent_map_end(em);
  4117. em_len = em_end - em_start;
  4118. disko = 0;
  4119. flags = 0;
  4120. /*
  4121. * bump off for our next call to get_extent
  4122. */
  4123. off = extent_map_end(em);
  4124. if (off >= max)
  4125. end = 1;
  4126. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  4127. end = 1;
  4128. flags |= FIEMAP_EXTENT_LAST;
  4129. } else if (em->block_start == EXTENT_MAP_INLINE) {
  4130. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  4131. FIEMAP_EXTENT_NOT_ALIGNED);
  4132. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  4133. flags |= (FIEMAP_EXTENT_DELALLOC |
  4134. FIEMAP_EXTENT_UNKNOWN);
  4135. } else if (fieinfo->fi_extents_max) {
  4136. struct btrfs_trans_handle *trans;
  4137. u64 bytenr = em->block_start -
  4138. (em->start - em->orig_start);
  4139. disko = em->block_start + offset_in_extent;
  4140. /*
  4141. * We need a trans handle to get delayed refs
  4142. */
  4143. trans = btrfs_join_transaction(root);
  4144. /*
  4145. * It's OK if we can't start a trans we can still check
  4146. * from commit_root
  4147. */
  4148. if (IS_ERR(trans))
  4149. trans = NULL;
  4150. /*
  4151. * As btrfs supports shared space, this information
  4152. * can be exported to userspace tools via
  4153. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  4154. * then we're just getting a count and we can skip the
  4155. * lookup stuff.
  4156. */
  4157. ret = btrfs_check_shared(trans, root->fs_info,
  4158. root->objectid,
  4159. btrfs_ino(BTRFS_I(inode)), bytenr);
  4160. if (trans)
  4161. btrfs_end_transaction(trans);
  4162. if (ret < 0)
  4163. goto out_free;
  4164. if (ret)
  4165. flags |= FIEMAP_EXTENT_SHARED;
  4166. ret = 0;
  4167. }
  4168. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4169. flags |= FIEMAP_EXTENT_ENCODED;
  4170. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4171. flags |= FIEMAP_EXTENT_UNWRITTEN;
  4172. free_extent_map(em);
  4173. em = NULL;
  4174. if ((em_start >= last) || em_len == (u64)-1 ||
  4175. (last == (u64)-1 && isize <= em_end)) {
  4176. flags |= FIEMAP_EXTENT_LAST;
  4177. end = 1;
  4178. }
  4179. /* now scan forward to see if this is really the last extent. */
  4180. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4181. get_extent);
  4182. if (IS_ERR(em)) {
  4183. ret = PTR_ERR(em);
  4184. goto out;
  4185. }
  4186. if (!em) {
  4187. flags |= FIEMAP_EXTENT_LAST;
  4188. end = 1;
  4189. }
  4190. ret = emit_fiemap_extent(fieinfo, &cache, em_start, disko,
  4191. em_len, flags);
  4192. if (ret) {
  4193. if (ret == 1)
  4194. ret = 0;
  4195. goto out_free;
  4196. }
  4197. }
  4198. out_free:
  4199. if (!ret)
  4200. ret = check_fiemap_cache(root->fs_info, fieinfo, &cache);
  4201. free_extent_map(em);
  4202. out:
  4203. btrfs_free_path(path);
  4204. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4205. &cached_state, GFP_NOFS);
  4206. return ret;
  4207. }
  4208. static void __free_extent_buffer(struct extent_buffer *eb)
  4209. {
  4210. btrfs_leak_debug_del(&eb->leak_list);
  4211. kmem_cache_free(extent_buffer_cache, eb);
  4212. }
  4213. int extent_buffer_under_io(struct extent_buffer *eb)
  4214. {
  4215. return (atomic_read(&eb->io_pages) ||
  4216. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4217. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4218. }
  4219. /*
  4220. * Helper for releasing extent buffer page.
  4221. */
  4222. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4223. {
  4224. unsigned long index;
  4225. struct page *page;
  4226. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4227. BUG_ON(extent_buffer_under_io(eb));
  4228. index = num_extent_pages(eb->start, eb->len);
  4229. if (index == 0)
  4230. return;
  4231. do {
  4232. index--;
  4233. page = eb->pages[index];
  4234. if (!page)
  4235. continue;
  4236. if (mapped)
  4237. spin_lock(&page->mapping->private_lock);
  4238. /*
  4239. * We do this since we'll remove the pages after we've
  4240. * removed the eb from the radix tree, so we could race
  4241. * and have this page now attached to the new eb. So
  4242. * only clear page_private if it's still connected to
  4243. * this eb.
  4244. */
  4245. if (PagePrivate(page) &&
  4246. page->private == (unsigned long)eb) {
  4247. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4248. BUG_ON(PageDirty(page));
  4249. BUG_ON(PageWriteback(page));
  4250. /*
  4251. * We need to make sure we haven't be attached
  4252. * to a new eb.
  4253. */
  4254. ClearPagePrivate(page);
  4255. set_page_private(page, 0);
  4256. /* One for the page private */
  4257. put_page(page);
  4258. }
  4259. if (mapped)
  4260. spin_unlock(&page->mapping->private_lock);
  4261. /* One for when we allocated the page */
  4262. put_page(page);
  4263. } while (index != 0);
  4264. }
  4265. /*
  4266. * Helper for releasing the extent buffer.
  4267. */
  4268. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4269. {
  4270. btrfs_release_extent_buffer_page(eb);
  4271. __free_extent_buffer(eb);
  4272. }
  4273. static struct extent_buffer *
  4274. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4275. unsigned long len)
  4276. {
  4277. struct extent_buffer *eb = NULL;
  4278. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
  4279. eb->start = start;
  4280. eb->len = len;
  4281. eb->fs_info = fs_info;
  4282. eb->bflags = 0;
  4283. rwlock_init(&eb->lock);
  4284. atomic_set(&eb->write_locks, 0);
  4285. atomic_set(&eb->read_locks, 0);
  4286. atomic_set(&eb->blocking_readers, 0);
  4287. atomic_set(&eb->blocking_writers, 0);
  4288. atomic_set(&eb->spinning_readers, 0);
  4289. atomic_set(&eb->spinning_writers, 0);
  4290. eb->lock_nested = 0;
  4291. init_waitqueue_head(&eb->write_lock_wq);
  4292. init_waitqueue_head(&eb->read_lock_wq);
  4293. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4294. spin_lock_init(&eb->refs_lock);
  4295. atomic_set(&eb->refs, 1);
  4296. atomic_set(&eb->io_pages, 0);
  4297. /*
  4298. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4299. */
  4300. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4301. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4302. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4303. return eb;
  4304. }
  4305. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4306. {
  4307. unsigned long i;
  4308. struct page *p;
  4309. struct extent_buffer *new;
  4310. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4311. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4312. if (new == NULL)
  4313. return NULL;
  4314. for (i = 0; i < num_pages; i++) {
  4315. p = alloc_page(GFP_NOFS);
  4316. if (!p) {
  4317. btrfs_release_extent_buffer(new);
  4318. return NULL;
  4319. }
  4320. attach_extent_buffer_page(new, p);
  4321. WARN_ON(PageDirty(p));
  4322. SetPageUptodate(p);
  4323. new->pages[i] = p;
  4324. copy_page(page_address(p), page_address(src->pages[i]));
  4325. }
  4326. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4327. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4328. return new;
  4329. }
  4330. struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4331. u64 start, unsigned long len)
  4332. {
  4333. struct extent_buffer *eb;
  4334. unsigned long num_pages;
  4335. unsigned long i;
  4336. num_pages = num_extent_pages(start, len);
  4337. eb = __alloc_extent_buffer(fs_info, start, len);
  4338. if (!eb)
  4339. return NULL;
  4340. for (i = 0; i < num_pages; i++) {
  4341. eb->pages[i] = alloc_page(GFP_NOFS);
  4342. if (!eb->pages[i])
  4343. goto err;
  4344. }
  4345. set_extent_buffer_uptodate(eb);
  4346. btrfs_set_header_nritems(eb, 0);
  4347. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4348. return eb;
  4349. err:
  4350. for (; i > 0; i--)
  4351. __free_page(eb->pages[i - 1]);
  4352. __free_extent_buffer(eb);
  4353. return NULL;
  4354. }
  4355. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4356. u64 start)
  4357. {
  4358. return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
  4359. }
  4360. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4361. {
  4362. int refs;
  4363. /* the ref bit is tricky. We have to make sure it is set
  4364. * if we have the buffer dirty. Otherwise the
  4365. * code to free a buffer can end up dropping a dirty
  4366. * page
  4367. *
  4368. * Once the ref bit is set, it won't go away while the
  4369. * buffer is dirty or in writeback, and it also won't
  4370. * go away while we have the reference count on the
  4371. * eb bumped.
  4372. *
  4373. * We can't just set the ref bit without bumping the
  4374. * ref on the eb because free_extent_buffer might
  4375. * see the ref bit and try to clear it. If this happens
  4376. * free_extent_buffer might end up dropping our original
  4377. * ref by mistake and freeing the page before we are able
  4378. * to add one more ref.
  4379. *
  4380. * So bump the ref count first, then set the bit. If someone
  4381. * beat us to it, drop the ref we added.
  4382. */
  4383. refs = atomic_read(&eb->refs);
  4384. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4385. return;
  4386. spin_lock(&eb->refs_lock);
  4387. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4388. atomic_inc(&eb->refs);
  4389. spin_unlock(&eb->refs_lock);
  4390. }
  4391. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4392. struct page *accessed)
  4393. {
  4394. unsigned long num_pages, i;
  4395. check_buffer_tree_ref(eb);
  4396. num_pages = num_extent_pages(eb->start, eb->len);
  4397. for (i = 0; i < num_pages; i++) {
  4398. struct page *p = eb->pages[i];
  4399. if (p != accessed)
  4400. mark_page_accessed(p);
  4401. }
  4402. }
  4403. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4404. u64 start)
  4405. {
  4406. struct extent_buffer *eb;
  4407. rcu_read_lock();
  4408. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4409. start >> PAGE_SHIFT);
  4410. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4411. rcu_read_unlock();
  4412. /*
  4413. * Lock our eb's refs_lock to avoid races with
  4414. * free_extent_buffer. When we get our eb it might be flagged
  4415. * with EXTENT_BUFFER_STALE and another task running
  4416. * free_extent_buffer might have seen that flag set,
  4417. * eb->refs == 2, that the buffer isn't under IO (dirty and
  4418. * writeback flags not set) and it's still in the tree (flag
  4419. * EXTENT_BUFFER_TREE_REF set), therefore being in the process
  4420. * of decrementing the extent buffer's reference count twice.
  4421. * So here we could race and increment the eb's reference count,
  4422. * clear its stale flag, mark it as dirty and drop our reference
  4423. * before the other task finishes executing free_extent_buffer,
  4424. * which would later result in an attempt to free an extent
  4425. * buffer that is dirty.
  4426. */
  4427. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  4428. spin_lock(&eb->refs_lock);
  4429. spin_unlock(&eb->refs_lock);
  4430. }
  4431. mark_extent_buffer_accessed(eb, NULL);
  4432. return eb;
  4433. }
  4434. rcu_read_unlock();
  4435. return NULL;
  4436. }
  4437. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4438. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4439. u64 start)
  4440. {
  4441. struct extent_buffer *eb, *exists = NULL;
  4442. int ret;
  4443. eb = find_extent_buffer(fs_info, start);
  4444. if (eb)
  4445. return eb;
  4446. eb = alloc_dummy_extent_buffer(fs_info, start);
  4447. if (!eb)
  4448. return NULL;
  4449. eb->fs_info = fs_info;
  4450. again:
  4451. ret = radix_tree_preload(GFP_NOFS);
  4452. if (ret)
  4453. goto free_eb;
  4454. spin_lock(&fs_info->buffer_lock);
  4455. ret = radix_tree_insert(&fs_info->buffer_radix,
  4456. start >> PAGE_SHIFT, eb);
  4457. spin_unlock(&fs_info->buffer_lock);
  4458. radix_tree_preload_end();
  4459. if (ret == -EEXIST) {
  4460. exists = find_extent_buffer(fs_info, start);
  4461. if (exists)
  4462. goto free_eb;
  4463. else
  4464. goto again;
  4465. }
  4466. check_buffer_tree_ref(eb);
  4467. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4468. /*
  4469. * We will free dummy extent buffer's if they come into
  4470. * free_extent_buffer with a ref count of 2, but if we are using this we
  4471. * want the buffers to stay in memory until we're done with them, so
  4472. * bump the ref count again.
  4473. */
  4474. atomic_inc(&eb->refs);
  4475. return eb;
  4476. free_eb:
  4477. btrfs_release_extent_buffer(eb);
  4478. return exists;
  4479. }
  4480. #endif
  4481. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4482. u64 start)
  4483. {
  4484. unsigned long len = fs_info->nodesize;
  4485. unsigned long num_pages = num_extent_pages(start, len);
  4486. unsigned long i;
  4487. unsigned long index = start >> PAGE_SHIFT;
  4488. struct extent_buffer *eb;
  4489. struct extent_buffer *exists = NULL;
  4490. struct page *p;
  4491. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4492. int uptodate = 1;
  4493. int ret;
  4494. if (!IS_ALIGNED(start, fs_info->sectorsize)) {
  4495. btrfs_err(fs_info, "bad tree block start %llu", start);
  4496. return ERR_PTR(-EINVAL);
  4497. }
  4498. eb = find_extent_buffer(fs_info, start);
  4499. if (eb)
  4500. return eb;
  4501. eb = __alloc_extent_buffer(fs_info, start, len);
  4502. if (!eb)
  4503. return ERR_PTR(-ENOMEM);
  4504. for (i = 0; i < num_pages; i++, index++) {
  4505. p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
  4506. if (!p) {
  4507. exists = ERR_PTR(-ENOMEM);
  4508. goto free_eb;
  4509. }
  4510. spin_lock(&mapping->private_lock);
  4511. if (PagePrivate(p)) {
  4512. /*
  4513. * We could have already allocated an eb for this page
  4514. * and attached one so lets see if we can get a ref on
  4515. * the existing eb, and if we can we know it's good and
  4516. * we can just return that one, else we know we can just
  4517. * overwrite page->private.
  4518. */
  4519. exists = (struct extent_buffer *)p->private;
  4520. if (atomic_inc_not_zero(&exists->refs)) {
  4521. spin_unlock(&mapping->private_lock);
  4522. unlock_page(p);
  4523. put_page(p);
  4524. mark_extent_buffer_accessed(exists, p);
  4525. goto free_eb;
  4526. }
  4527. exists = NULL;
  4528. /*
  4529. * Do this so attach doesn't complain and we need to
  4530. * drop the ref the old guy had.
  4531. */
  4532. ClearPagePrivate(p);
  4533. WARN_ON(PageDirty(p));
  4534. put_page(p);
  4535. }
  4536. attach_extent_buffer_page(eb, p);
  4537. spin_unlock(&mapping->private_lock);
  4538. WARN_ON(PageDirty(p));
  4539. eb->pages[i] = p;
  4540. if (!PageUptodate(p))
  4541. uptodate = 0;
  4542. /*
  4543. * see below about how we avoid a nasty race with release page
  4544. * and why we unlock later
  4545. */
  4546. }
  4547. if (uptodate)
  4548. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4549. again:
  4550. ret = radix_tree_preload(GFP_NOFS);
  4551. if (ret) {
  4552. exists = ERR_PTR(ret);
  4553. goto free_eb;
  4554. }
  4555. spin_lock(&fs_info->buffer_lock);
  4556. ret = radix_tree_insert(&fs_info->buffer_radix,
  4557. start >> PAGE_SHIFT, eb);
  4558. spin_unlock(&fs_info->buffer_lock);
  4559. radix_tree_preload_end();
  4560. if (ret == -EEXIST) {
  4561. exists = find_extent_buffer(fs_info, start);
  4562. if (exists)
  4563. goto free_eb;
  4564. else
  4565. goto again;
  4566. }
  4567. /* add one reference for the tree */
  4568. check_buffer_tree_ref(eb);
  4569. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4570. /*
  4571. * there is a race where release page may have
  4572. * tried to find this extent buffer in the radix
  4573. * but failed. It will tell the VM it is safe to
  4574. * reclaim the, and it will clear the page private bit.
  4575. * We must make sure to set the page private bit properly
  4576. * after the extent buffer is in the radix tree so
  4577. * it doesn't get lost
  4578. */
  4579. SetPageChecked(eb->pages[0]);
  4580. for (i = 1; i < num_pages; i++) {
  4581. p = eb->pages[i];
  4582. ClearPageChecked(p);
  4583. unlock_page(p);
  4584. }
  4585. unlock_page(eb->pages[0]);
  4586. return eb;
  4587. free_eb:
  4588. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4589. for (i = 0; i < num_pages; i++) {
  4590. if (eb->pages[i])
  4591. unlock_page(eb->pages[i]);
  4592. }
  4593. btrfs_release_extent_buffer(eb);
  4594. return exists;
  4595. }
  4596. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4597. {
  4598. struct extent_buffer *eb =
  4599. container_of(head, struct extent_buffer, rcu_head);
  4600. __free_extent_buffer(eb);
  4601. }
  4602. /* Expects to have eb->eb_lock already held */
  4603. static int release_extent_buffer(struct extent_buffer *eb)
  4604. {
  4605. WARN_ON(atomic_read(&eb->refs) == 0);
  4606. if (atomic_dec_and_test(&eb->refs)) {
  4607. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4608. struct btrfs_fs_info *fs_info = eb->fs_info;
  4609. spin_unlock(&eb->refs_lock);
  4610. spin_lock(&fs_info->buffer_lock);
  4611. radix_tree_delete(&fs_info->buffer_radix,
  4612. eb->start >> PAGE_SHIFT);
  4613. spin_unlock(&fs_info->buffer_lock);
  4614. } else {
  4615. spin_unlock(&eb->refs_lock);
  4616. }
  4617. /* Should be safe to release our pages at this point */
  4618. btrfs_release_extent_buffer_page(eb);
  4619. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4620. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4621. __free_extent_buffer(eb);
  4622. return 1;
  4623. }
  4624. #endif
  4625. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4626. return 1;
  4627. }
  4628. spin_unlock(&eb->refs_lock);
  4629. return 0;
  4630. }
  4631. void free_extent_buffer(struct extent_buffer *eb)
  4632. {
  4633. int refs;
  4634. int old;
  4635. if (!eb)
  4636. return;
  4637. while (1) {
  4638. refs = atomic_read(&eb->refs);
  4639. if (refs <= 3)
  4640. break;
  4641. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4642. if (old == refs)
  4643. return;
  4644. }
  4645. spin_lock(&eb->refs_lock);
  4646. if (atomic_read(&eb->refs) == 2 &&
  4647. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4648. atomic_dec(&eb->refs);
  4649. if (atomic_read(&eb->refs) == 2 &&
  4650. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4651. !extent_buffer_under_io(eb) &&
  4652. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4653. atomic_dec(&eb->refs);
  4654. /*
  4655. * I know this is terrible, but it's temporary until we stop tracking
  4656. * the uptodate bits and such for the extent buffers.
  4657. */
  4658. release_extent_buffer(eb);
  4659. }
  4660. void free_extent_buffer_stale(struct extent_buffer *eb)
  4661. {
  4662. if (!eb)
  4663. return;
  4664. spin_lock(&eb->refs_lock);
  4665. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4666. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4667. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4668. atomic_dec(&eb->refs);
  4669. release_extent_buffer(eb);
  4670. }
  4671. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4672. {
  4673. unsigned long i;
  4674. unsigned long num_pages;
  4675. struct page *page;
  4676. num_pages = num_extent_pages(eb->start, eb->len);
  4677. for (i = 0; i < num_pages; i++) {
  4678. page = eb->pages[i];
  4679. if (!PageDirty(page))
  4680. continue;
  4681. lock_page(page);
  4682. WARN_ON(!PagePrivate(page));
  4683. clear_page_dirty_for_io(page);
  4684. spin_lock_irq(&page->mapping->tree_lock);
  4685. if (!PageDirty(page)) {
  4686. radix_tree_tag_clear(&page->mapping->page_tree,
  4687. page_index(page),
  4688. PAGECACHE_TAG_DIRTY);
  4689. }
  4690. spin_unlock_irq(&page->mapping->tree_lock);
  4691. ClearPageError(page);
  4692. unlock_page(page);
  4693. }
  4694. WARN_ON(atomic_read(&eb->refs) == 0);
  4695. }
  4696. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4697. {
  4698. unsigned long i;
  4699. unsigned long num_pages;
  4700. int was_dirty = 0;
  4701. check_buffer_tree_ref(eb);
  4702. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4703. num_pages = num_extent_pages(eb->start, eb->len);
  4704. WARN_ON(atomic_read(&eb->refs) == 0);
  4705. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4706. for (i = 0; i < num_pages; i++)
  4707. set_page_dirty(eb->pages[i]);
  4708. return was_dirty;
  4709. }
  4710. void clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4711. {
  4712. unsigned long i;
  4713. struct page *page;
  4714. unsigned long num_pages;
  4715. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4716. num_pages = num_extent_pages(eb->start, eb->len);
  4717. for (i = 0; i < num_pages; i++) {
  4718. page = eb->pages[i];
  4719. if (page)
  4720. ClearPageUptodate(page);
  4721. }
  4722. }
  4723. void set_extent_buffer_uptodate(struct extent_buffer *eb)
  4724. {
  4725. unsigned long i;
  4726. struct page *page;
  4727. unsigned long num_pages;
  4728. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4729. num_pages = num_extent_pages(eb->start, eb->len);
  4730. for (i = 0; i < num_pages; i++) {
  4731. page = eb->pages[i];
  4732. SetPageUptodate(page);
  4733. }
  4734. }
  4735. int extent_buffer_uptodate(struct extent_buffer *eb)
  4736. {
  4737. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4738. }
  4739. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4740. struct extent_buffer *eb, int wait,
  4741. get_extent_t *get_extent, int mirror_num)
  4742. {
  4743. unsigned long i;
  4744. struct page *page;
  4745. int err;
  4746. int ret = 0;
  4747. int locked_pages = 0;
  4748. int all_uptodate = 1;
  4749. unsigned long num_pages;
  4750. unsigned long num_reads = 0;
  4751. struct bio *bio = NULL;
  4752. unsigned long bio_flags = 0;
  4753. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4754. return 0;
  4755. num_pages = num_extent_pages(eb->start, eb->len);
  4756. for (i = 0; i < num_pages; i++) {
  4757. page = eb->pages[i];
  4758. if (wait == WAIT_NONE) {
  4759. if (!trylock_page(page))
  4760. goto unlock_exit;
  4761. } else {
  4762. lock_page(page);
  4763. }
  4764. locked_pages++;
  4765. }
  4766. /*
  4767. * We need to firstly lock all pages to make sure that
  4768. * the uptodate bit of our pages won't be affected by
  4769. * clear_extent_buffer_uptodate().
  4770. */
  4771. for (i = 0; i < num_pages; i++) {
  4772. page = eb->pages[i];
  4773. if (!PageUptodate(page)) {
  4774. num_reads++;
  4775. all_uptodate = 0;
  4776. }
  4777. }
  4778. if (all_uptodate) {
  4779. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4780. goto unlock_exit;
  4781. }
  4782. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4783. eb->read_mirror = 0;
  4784. atomic_set(&eb->io_pages, num_reads);
  4785. for (i = 0; i < num_pages; i++) {
  4786. page = eb->pages[i];
  4787. if (!PageUptodate(page)) {
  4788. if (ret) {
  4789. atomic_dec(&eb->io_pages);
  4790. unlock_page(page);
  4791. continue;
  4792. }
  4793. ClearPageError(page);
  4794. err = __extent_read_full_page(tree, page,
  4795. get_extent, &bio,
  4796. mirror_num, &bio_flags,
  4797. REQ_META);
  4798. if (err) {
  4799. ret = err;
  4800. /*
  4801. * We use &bio in above __extent_read_full_page,
  4802. * so we ensure that if it returns error, the
  4803. * current page fails to add itself to bio and
  4804. * it's been unlocked.
  4805. *
  4806. * We must dec io_pages by ourselves.
  4807. */
  4808. atomic_dec(&eb->io_pages);
  4809. }
  4810. } else {
  4811. unlock_page(page);
  4812. }
  4813. }
  4814. if (bio) {
  4815. err = submit_one_bio(bio, mirror_num, bio_flags);
  4816. if (err)
  4817. return err;
  4818. }
  4819. if (ret || wait != WAIT_COMPLETE)
  4820. return ret;
  4821. for (i = 0; i < num_pages; i++) {
  4822. page = eb->pages[i];
  4823. wait_on_page_locked(page);
  4824. if (!PageUptodate(page))
  4825. ret = -EIO;
  4826. }
  4827. return ret;
  4828. unlock_exit:
  4829. while (locked_pages > 0) {
  4830. locked_pages--;
  4831. page = eb->pages[locked_pages];
  4832. unlock_page(page);
  4833. }
  4834. return ret;
  4835. }
  4836. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4837. unsigned long start,
  4838. unsigned long len)
  4839. {
  4840. size_t cur;
  4841. size_t offset;
  4842. struct page *page;
  4843. char *kaddr;
  4844. char *dst = (char *)dstv;
  4845. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4846. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4847. WARN_ON(start > eb->len);
  4848. WARN_ON(start + len > eb->start + eb->len);
  4849. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4850. while (len > 0) {
  4851. page = eb->pages[i];
  4852. cur = min(len, (PAGE_SIZE - offset));
  4853. kaddr = page_address(page);
  4854. memcpy(dst, kaddr + offset, cur);
  4855. dst += cur;
  4856. len -= cur;
  4857. offset = 0;
  4858. i++;
  4859. }
  4860. }
  4861. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
  4862. unsigned long start,
  4863. unsigned long len)
  4864. {
  4865. size_t cur;
  4866. size_t offset;
  4867. struct page *page;
  4868. char *kaddr;
  4869. char __user *dst = (char __user *)dstv;
  4870. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4871. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4872. int ret = 0;
  4873. WARN_ON(start > eb->len);
  4874. WARN_ON(start + len > eb->start + eb->len);
  4875. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4876. while (len > 0) {
  4877. page = eb->pages[i];
  4878. cur = min(len, (PAGE_SIZE - offset));
  4879. kaddr = page_address(page);
  4880. if (copy_to_user(dst, kaddr + offset, cur)) {
  4881. ret = -EFAULT;
  4882. break;
  4883. }
  4884. dst += cur;
  4885. len -= cur;
  4886. offset = 0;
  4887. i++;
  4888. }
  4889. return ret;
  4890. }
  4891. /*
  4892. * return 0 if the item is found within a page.
  4893. * return 1 if the item spans two pages.
  4894. * return -EINVAL otherwise.
  4895. */
  4896. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4897. unsigned long min_len, char **map,
  4898. unsigned long *map_start,
  4899. unsigned long *map_len)
  4900. {
  4901. size_t offset = start & (PAGE_SIZE - 1);
  4902. char *kaddr;
  4903. struct page *p;
  4904. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4905. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4906. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4907. PAGE_SHIFT;
  4908. if (i != end_i)
  4909. return 1;
  4910. if (i == 0) {
  4911. offset = start_offset;
  4912. *map_start = 0;
  4913. } else {
  4914. offset = 0;
  4915. *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
  4916. }
  4917. if (start + min_len > eb->len) {
  4918. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
  4919. eb->start, eb->len, start, min_len);
  4920. return -EINVAL;
  4921. }
  4922. p = eb->pages[i];
  4923. kaddr = page_address(p);
  4924. *map = kaddr + offset;
  4925. *map_len = PAGE_SIZE - offset;
  4926. return 0;
  4927. }
  4928. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4929. unsigned long start,
  4930. unsigned long len)
  4931. {
  4932. size_t cur;
  4933. size_t offset;
  4934. struct page *page;
  4935. char *kaddr;
  4936. char *ptr = (char *)ptrv;
  4937. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4938. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4939. int ret = 0;
  4940. WARN_ON(start > eb->len);
  4941. WARN_ON(start + len > eb->start + eb->len);
  4942. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4943. while (len > 0) {
  4944. page = eb->pages[i];
  4945. cur = min(len, (PAGE_SIZE - offset));
  4946. kaddr = page_address(page);
  4947. ret = memcmp(ptr, kaddr + offset, cur);
  4948. if (ret)
  4949. break;
  4950. ptr += cur;
  4951. len -= cur;
  4952. offset = 0;
  4953. i++;
  4954. }
  4955. return ret;
  4956. }
  4957. void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
  4958. const void *srcv)
  4959. {
  4960. char *kaddr;
  4961. WARN_ON(!PageUptodate(eb->pages[0]));
  4962. kaddr = page_address(eb->pages[0]);
  4963. memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
  4964. BTRFS_FSID_SIZE);
  4965. }
  4966. void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
  4967. {
  4968. char *kaddr;
  4969. WARN_ON(!PageUptodate(eb->pages[0]));
  4970. kaddr = page_address(eb->pages[0]);
  4971. memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
  4972. BTRFS_FSID_SIZE);
  4973. }
  4974. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4975. unsigned long start, unsigned long len)
  4976. {
  4977. size_t cur;
  4978. size_t offset;
  4979. struct page *page;
  4980. char *kaddr;
  4981. char *src = (char *)srcv;
  4982. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4983. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4984. WARN_ON(start > eb->len);
  4985. WARN_ON(start + len > eb->start + eb->len);
  4986. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4987. while (len > 0) {
  4988. page = eb->pages[i];
  4989. WARN_ON(!PageUptodate(page));
  4990. cur = min(len, PAGE_SIZE - offset);
  4991. kaddr = page_address(page);
  4992. memcpy(kaddr + offset, src, cur);
  4993. src += cur;
  4994. len -= cur;
  4995. offset = 0;
  4996. i++;
  4997. }
  4998. }
  4999. void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
  5000. unsigned long len)
  5001. {
  5002. size_t cur;
  5003. size_t offset;
  5004. struct page *page;
  5005. char *kaddr;
  5006. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  5007. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  5008. WARN_ON(start > eb->len);
  5009. WARN_ON(start + len > eb->start + eb->len);
  5010. offset = (start_offset + start) & (PAGE_SIZE - 1);
  5011. while (len > 0) {
  5012. page = eb->pages[i];
  5013. WARN_ON(!PageUptodate(page));
  5014. cur = min(len, PAGE_SIZE - offset);
  5015. kaddr = page_address(page);
  5016. memset(kaddr + offset, 0, cur);
  5017. len -= cur;
  5018. offset = 0;
  5019. i++;
  5020. }
  5021. }
  5022. void copy_extent_buffer_full(struct extent_buffer *dst,
  5023. struct extent_buffer *src)
  5024. {
  5025. int i;
  5026. unsigned num_pages;
  5027. ASSERT(dst->len == src->len);
  5028. num_pages = num_extent_pages(dst->start, dst->len);
  5029. for (i = 0; i < num_pages; i++)
  5030. copy_page(page_address(dst->pages[i]),
  5031. page_address(src->pages[i]));
  5032. }
  5033. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  5034. unsigned long dst_offset, unsigned long src_offset,
  5035. unsigned long len)
  5036. {
  5037. u64 dst_len = dst->len;
  5038. size_t cur;
  5039. size_t offset;
  5040. struct page *page;
  5041. char *kaddr;
  5042. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5043. unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5044. WARN_ON(src->len != dst_len);
  5045. offset = (start_offset + dst_offset) &
  5046. (PAGE_SIZE - 1);
  5047. while (len > 0) {
  5048. page = dst->pages[i];
  5049. WARN_ON(!PageUptodate(page));
  5050. cur = min(len, (unsigned long)(PAGE_SIZE - offset));
  5051. kaddr = page_address(page);
  5052. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  5053. src_offset += cur;
  5054. len -= cur;
  5055. offset = 0;
  5056. i++;
  5057. }
  5058. }
  5059. void le_bitmap_set(u8 *map, unsigned int start, int len)
  5060. {
  5061. u8 *p = map + BIT_BYTE(start);
  5062. const unsigned int size = start + len;
  5063. int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  5064. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
  5065. while (len - bits_to_set >= 0) {
  5066. *p |= mask_to_set;
  5067. len -= bits_to_set;
  5068. bits_to_set = BITS_PER_BYTE;
  5069. mask_to_set = ~0;
  5070. p++;
  5071. }
  5072. if (len) {
  5073. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5074. *p |= mask_to_set;
  5075. }
  5076. }
  5077. void le_bitmap_clear(u8 *map, unsigned int start, int len)
  5078. {
  5079. u8 *p = map + BIT_BYTE(start);
  5080. const unsigned int size = start + len;
  5081. int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  5082. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
  5083. while (len - bits_to_clear >= 0) {
  5084. *p &= ~mask_to_clear;
  5085. len -= bits_to_clear;
  5086. bits_to_clear = BITS_PER_BYTE;
  5087. mask_to_clear = ~0;
  5088. p++;
  5089. }
  5090. if (len) {
  5091. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5092. *p &= ~mask_to_clear;
  5093. }
  5094. }
  5095. /*
  5096. * eb_bitmap_offset() - calculate the page and offset of the byte containing the
  5097. * given bit number
  5098. * @eb: the extent buffer
  5099. * @start: offset of the bitmap item in the extent buffer
  5100. * @nr: bit number
  5101. * @page_index: return index of the page in the extent buffer that contains the
  5102. * given bit number
  5103. * @page_offset: return offset into the page given by page_index
  5104. *
  5105. * This helper hides the ugliness of finding the byte in an extent buffer which
  5106. * contains a given bit.
  5107. */
  5108. static inline void eb_bitmap_offset(struct extent_buffer *eb,
  5109. unsigned long start, unsigned long nr,
  5110. unsigned long *page_index,
  5111. size_t *page_offset)
  5112. {
  5113. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  5114. size_t byte_offset = BIT_BYTE(nr);
  5115. size_t offset;
  5116. /*
  5117. * The byte we want is the offset of the extent buffer + the offset of
  5118. * the bitmap item in the extent buffer + the offset of the byte in the
  5119. * bitmap item.
  5120. */
  5121. offset = start_offset + start + byte_offset;
  5122. *page_index = offset >> PAGE_SHIFT;
  5123. *page_offset = offset & (PAGE_SIZE - 1);
  5124. }
  5125. /**
  5126. * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
  5127. * @eb: the extent buffer
  5128. * @start: offset of the bitmap item in the extent buffer
  5129. * @nr: bit number to test
  5130. */
  5131. int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
  5132. unsigned long nr)
  5133. {
  5134. u8 *kaddr;
  5135. struct page *page;
  5136. unsigned long i;
  5137. size_t offset;
  5138. eb_bitmap_offset(eb, start, nr, &i, &offset);
  5139. page = eb->pages[i];
  5140. WARN_ON(!PageUptodate(page));
  5141. kaddr = page_address(page);
  5142. return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
  5143. }
  5144. /**
  5145. * extent_buffer_bitmap_set - set an area of a bitmap
  5146. * @eb: the extent buffer
  5147. * @start: offset of the bitmap item in the extent buffer
  5148. * @pos: bit number of the first bit
  5149. * @len: number of bits to set
  5150. */
  5151. void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
  5152. unsigned long pos, unsigned long len)
  5153. {
  5154. u8 *kaddr;
  5155. struct page *page;
  5156. unsigned long i;
  5157. size_t offset;
  5158. const unsigned int size = pos + len;
  5159. int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5160. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
  5161. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5162. page = eb->pages[i];
  5163. WARN_ON(!PageUptodate(page));
  5164. kaddr = page_address(page);
  5165. while (len >= bits_to_set) {
  5166. kaddr[offset] |= mask_to_set;
  5167. len -= bits_to_set;
  5168. bits_to_set = BITS_PER_BYTE;
  5169. mask_to_set = ~0;
  5170. if (++offset >= PAGE_SIZE && len > 0) {
  5171. offset = 0;
  5172. page = eb->pages[++i];
  5173. WARN_ON(!PageUptodate(page));
  5174. kaddr = page_address(page);
  5175. }
  5176. }
  5177. if (len) {
  5178. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5179. kaddr[offset] |= mask_to_set;
  5180. }
  5181. }
  5182. /**
  5183. * extent_buffer_bitmap_clear - clear an area of a bitmap
  5184. * @eb: the extent buffer
  5185. * @start: offset of the bitmap item in the extent buffer
  5186. * @pos: bit number of the first bit
  5187. * @len: number of bits to clear
  5188. */
  5189. void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
  5190. unsigned long pos, unsigned long len)
  5191. {
  5192. u8 *kaddr;
  5193. struct page *page;
  5194. unsigned long i;
  5195. size_t offset;
  5196. const unsigned int size = pos + len;
  5197. int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5198. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
  5199. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5200. page = eb->pages[i];
  5201. WARN_ON(!PageUptodate(page));
  5202. kaddr = page_address(page);
  5203. while (len >= bits_to_clear) {
  5204. kaddr[offset] &= ~mask_to_clear;
  5205. len -= bits_to_clear;
  5206. bits_to_clear = BITS_PER_BYTE;
  5207. mask_to_clear = ~0;
  5208. if (++offset >= PAGE_SIZE && len > 0) {
  5209. offset = 0;
  5210. page = eb->pages[++i];
  5211. WARN_ON(!PageUptodate(page));
  5212. kaddr = page_address(page);
  5213. }
  5214. }
  5215. if (len) {
  5216. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5217. kaddr[offset] &= ~mask_to_clear;
  5218. }
  5219. }
  5220. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  5221. {
  5222. unsigned long distance = (src > dst) ? src - dst : dst - src;
  5223. return distance < len;
  5224. }
  5225. static void copy_pages(struct page *dst_page, struct page *src_page,
  5226. unsigned long dst_off, unsigned long src_off,
  5227. unsigned long len)
  5228. {
  5229. char *dst_kaddr = page_address(dst_page);
  5230. char *src_kaddr;
  5231. int must_memmove = 0;
  5232. if (dst_page != src_page) {
  5233. src_kaddr = page_address(src_page);
  5234. } else {
  5235. src_kaddr = dst_kaddr;
  5236. if (areas_overlap(src_off, dst_off, len))
  5237. must_memmove = 1;
  5238. }
  5239. if (must_memmove)
  5240. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5241. else
  5242. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5243. }
  5244. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5245. unsigned long src_offset, unsigned long len)
  5246. {
  5247. struct btrfs_fs_info *fs_info = dst->fs_info;
  5248. size_t cur;
  5249. size_t dst_off_in_page;
  5250. size_t src_off_in_page;
  5251. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5252. unsigned long dst_i;
  5253. unsigned long src_i;
  5254. if (src_offset + len > dst->len) {
  5255. btrfs_err(fs_info,
  5256. "memmove bogus src_offset %lu move len %lu dst len %lu",
  5257. src_offset, len, dst->len);
  5258. BUG_ON(1);
  5259. }
  5260. if (dst_offset + len > dst->len) {
  5261. btrfs_err(fs_info,
  5262. "memmove bogus dst_offset %lu move len %lu dst len %lu",
  5263. dst_offset, len, dst->len);
  5264. BUG_ON(1);
  5265. }
  5266. while (len > 0) {
  5267. dst_off_in_page = (start_offset + dst_offset) &
  5268. (PAGE_SIZE - 1);
  5269. src_off_in_page = (start_offset + src_offset) &
  5270. (PAGE_SIZE - 1);
  5271. dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5272. src_i = (start_offset + src_offset) >> PAGE_SHIFT;
  5273. cur = min(len, (unsigned long)(PAGE_SIZE -
  5274. src_off_in_page));
  5275. cur = min_t(unsigned long, cur,
  5276. (unsigned long)(PAGE_SIZE - dst_off_in_page));
  5277. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5278. dst_off_in_page, src_off_in_page, cur);
  5279. src_offset += cur;
  5280. dst_offset += cur;
  5281. len -= cur;
  5282. }
  5283. }
  5284. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5285. unsigned long src_offset, unsigned long len)
  5286. {
  5287. struct btrfs_fs_info *fs_info = dst->fs_info;
  5288. size_t cur;
  5289. size_t dst_off_in_page;
  5290. size_t src_off_in_page;
  5291. unsigned long dst_end = dst_offset + len - 1;
  5292. unsigned long src_end = src_offset + len - 1;
  5293. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5294. unsigned long dst_i;
  5295. unsigned long src_i;
  5296. if (src_offset + len > dst->len) {
  5297. btrfs_err(fs_info,
  5298. "memmove bogus src_offset %lu move len %lu len %lu",
  5299. src_offset, len, dst->len);
  5300. BUG_ON(1);
  5301. }
  5302. if (dst_offset + len > dst->len) {
  5303. btrfs_err(fs_info,
  5304. "memmove bogus dst_offset %lu move len %lu len %lu",
  5305. dst_offset, len, dst->len);
  5306. BUG_ON(1);
  5307. }
  5308. if (dst_offset < src_offset) {
  5309. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  5310. return;
  5311. }
  5312. while (len > 0) {
  5313. dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
  5314. src_i = (start_offset + src_end) >> PAGE_SHIFT;
  5315. dst_off_in_page = (start_offset + dst_end) &
  5316. (PAGE_SIZE - 1);
  5317. src_off_in_page = (start_offset + src_end) &
  5318. (PAGE_SIZE - 1);
  5319. cur = min_t(unsigned long, len, src_off_in_page + 1);
  5320. cur = min(cur, dst_off_in_page + 1);
  5321. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5322. dst_off_in_page - cur + 1,
  5323. src_off_in_page - cur + 1, cur);
  5324. dst_end -= cur;
  5325. src_end -= cur;
  5326. len -= cur;
  5327. }
  5328. }
  5329. int try_release_extent_buffer(struct page *page)
  5330. {
  5331. struct extent_buffer *eb;
  5332. /*
  5333. * We need to make sure nobody is attaching this page to an eb right
  5334. * now.
  5335. */
  5336. spin_lock(&page->mapping->private_lock);
  5337. if (!PagePrivate(page)) {
  5338. spin_unlock(&page->mapping->private_lock);
  5339. return 1;
  5340. }
  5341. eb = (struct extent_buffer *)page->private;
  5342. BUG_ON(!eb);
  5343. /*
  5344. * This is a little awful but should be ok, we need to make sure that
  5345. * the eb doesn't disappear out from under us while we're looking at
  5346. * this page.
  5347. */
  5348. spin_lock(&eb->refs_lock);
  5349. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  5350. spin_unlock(&eb->refs_lock);
  5351. spin_unlock(&page->mapping->private_lock);
  5352. return 0;
  5353. }
  5354. spin_unlock(&page->mapping->private_lock);
  5355. /*
  5356. * If tree ref isn't set then we know the ref on this eb is a real ref,
  5357. * so just return, this page will likely be freed soon anyway.
  5358. */
  5359. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  5360. spin_unlock(&eb->refs_lock);
  5361. return 0;
  5362. }
  5363. return release_extent_buffer(eb);
  5364. }