extent_io.c 150 KB

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