extent_io.c 147 KB

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