extent_io.c 149 KB

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