extent_io.c 147 KB

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