extent_io.c 152 KB

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