extent_io.c 154 KB

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