extent_io.c 152 KB

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