extent_io.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828
  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_RECLAIM_ACCOUNT | 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_RECLAIM_ACCOUNT | 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_add_page(bio, page, length, pg_offset);
  1842. if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) {
  1843. /* try to remap that extent elsewhere? */
  1844. btrfs_bio_counter_dec(fs_info);
  1845. bio_put(bio);
  1846. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1847. return -EIO;
  1848. }
  1849. btrfs_info_rl_in_rcu(fs_info,
  1850. "read error corrected: ino %llu off %llu (dev %s sector %llu)",
  1851. btrfs_ino(inode), start,
  1852. rcu_str_deref(dev->name), sector);
  1853. btrfs_bio_counter_dec(fs_info);
  1854. bio_put(bio);
  1855. return 0;
  1856. }
  1857. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  1858. int mirror_num)
  1859. {
  1860. u64 start = eb->start;
  1861. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1862. int ret = 0;
  1863. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1864. return -EROFS;
  1865. for (i = 0; i < num_pages; i++) {
  1866. struct page *p = eb->pages[i];
  1867. ret = repair_io_failure(root->fs_info->btree_inode, start,
  1868. PAGE_SIZE, start, p,
  1869. start - page_offset(p), mirror_num);
  1870. if (ret)
  1871. break;
  1872. start += PAGE_SIZE;
  1873. }
  1874. return ret;
  1875. }
  1876. /*
  1877. * each time an IO finishes, we do a fast check in the IO failure tree
  1878. * to see if we need to process or clean up an io_failure_record
  1879. */
  1880. int clean_io_failure(struct inode *inode, u64 start, struct page *page,
  1881. unsigned int pg_offset)
  1882. {
  1883. u64 private;
  1884. struct io_failure_record *failrec;
  1885. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1886. struct extent_state *state;
  1887. int num_copies;
  1888. int ret;
  1889. private = 0;
  1890. ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1891. (u64)-1, 1, EXTENT_DIRTY, 0);
  1892. if (!ret)
  1893. return 0;
  1894. ret = get_state_failrec(&BTRFS_I(inode)->io_failure_tree, start,
  1895. &failrec);
  1896. if (ret)
  1897. return 0;
  1898. BUG_ON(!failrec->this_mirror);
  1899. if (failrec->in_validation) {
  1900. /* there was no real error, just free the record */
  1901. pr_debug("clean_io_failure: freeing dummy error at %llu\n",
  1902. failrec->start);
  1903. goto out;
  1904. }
  1905. if (fs_info->sb->s_flags & MS_RDONLY)
  1906. goto out;
  1907. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1908. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1909. failrec->start,
  1910. EXTENT_LOCKED);
  1911. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1912. if (state && state->start <= failrec->start &&
  1913. state->end >= failrec->start + failrec->len - 1) {
  1914. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1915. failrec->len);
  1916. if (num_copies > 1) {
  1917. repair_io_failure(inode, start, failrec->len,
  1918. failrec->logical, page,
  1919. pg_offset, failrec->failed_mirror);
  1920. }
  1921. }
  1922. out:
  1923. free_io_failure(inode, failrec);
  1924. return 0;
  1925. }
  1926. /*
  1927. * Can be called when
  1928. * - hold extent lock
  1929. * - under ordered extent
  1930. * - the inode is freeing
  1931. */
  1932. void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end)
  1933. {
  1934. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1935. struct io_failure_record *failrec;
  1936. struct extent_state *state, *next;
  1937. if (RB_EMPTY_ROOT(&failure_tree->state))
  1938. return;
  1939. spin_lock(&failure_tree->lock);
  1940. state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
  1941. while (state) {
  1942. if (state->start > end)
  1943. break;
  1944. ASSERT(state->end <= end);
  1945. next = next_state(state);
  1946. failrec = state->failrec;
  1947. free_extent_state(state);
  1948. kfree(failrec);
  1949. state = next;
  1950. }
  1951. spin_unlock(&failure_tree->lock);
  1952. }
  1953. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  1954. struct io_failure_record **failrec_ret)
  1955. {
  1956. struct io_failure_record *failrec;
  1957. struct extent_map *em;
  1958. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1959. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1960. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1961. int ret;
  1962. u64 logical;
  1963. ret = get_state_failrec(failure_tree, start, &failrec);
  1964. if (ret) {
  1965. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1966. if (!failrec)
  1967. return -ENOMEM;
  1968. failrec->start = start;
  1969. failrec->len = end - start + 1;
  1970. failrec->this_mirror = 0;
  1971. failrec->bio_flags = 0;
  1972. failrec->in_validation = 0;
  1973. read_lock(&em_tree->lock);
  1974. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1975. if (!em) {
  1976. read_unlock(&em_tree->lock);
  1977. kfree(failrec);
  1978. return -EIO;
  1979. }
  1980. if (em->start > start || em->start + em->len <= start) {
  1981. free_extent_map(em);
  1982. em = NULL;
  1983. }
  1984. read_unlock(&em_tree->lock);
  1985. if (!em) {
  1986. kfree(failrec);
  1987. return -EIO;
  1988. }
  1989. logical = start - em->start;
  1990. logical = em->block_start + logical;
  1991. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1992. logical = em->block_start;
  1993. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1994. extent_set_compress_type(&failrec->bio_flags,
  1995. em->compress_type);
  1996. }
  1997. pr_debug("Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu\n",
  1998. logical, start, failrec->len);
  1999. failrec->logical = logical;
  2000. free_extent_map(em);
  2001. /* set the bits in the private failure tree */
  2002. ret = set_extent_bits(failure_tree, start, end,
  2003. EXTENT_LOCKED | EXTENT_DIRTY);
  2004. if (ret >= 0)
  2005. ret = set_state_failrec(failure_tree, start, failrec);
  2006. /* set the bits in the inode's tree */
  2007. if (ret >= 0)
  2008. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
  2009. if (ret < 0) {
  2010. kfree(failrec);
  2011. return ret;
  2012. }
  2013. } else {
  2014. pr_debug("Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d\n",
  2015. failrec->logical, failrec->start, failrec->len,
  2016. failrec->in_validation);
  2017. /*
  2018. * when data can be on disk more than twice, add to failrec here
  2019. * (e.g. with a list for failed_mirror) to make
  2020. * clean_io_failure() clean all those errors at once.
  2021. */
  2022. }
  2023. *failrec_ret = failrec;
  2024. return 0;
  2025. }
  2026. int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  2027. struct io_failure_record *failrec, int failed_mirror)
  2028. {
  2029. int num_copies;
  2030. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  2031. failrec->logical, failrec->len);
  2032. if (num_copies == 1) {
  2033. /*
  2034. * we only have a single copy of the data, so don't bother with
  2035. * all the retry and error correction code that follows. no
  2036. * matter what the error is, it is very likely to persist.
  2037. */
  2038. pr_debug("Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
  2039. num_copies, failrec->this_mirror, failed_mirror);
  2040. return 0;
  2041. }
  2042. /*
  2043. * there are two premises:
  2044. * a) deliver good data to the caller
  2045. * b) correct the bad sectors on disk
  2046. */
  2047. if (failed_bio->bi_vcnt > 1) {
  2048. /*
  2049. * to fulfill b), we need to know the exact failing sectors, as
  2050. * we don't want to rewrite any more than the failed ones. thus,
  2051. * we need separate read requests for the failed bio
  2052. *
  2053. * if the following BUG_ON triggers, our validation request got
  2054. * merged. we need separate requests for our algorithm to work.
  2055. */
  2056. BUG_ON(failrec->in_validation);
  2057. failrec->in_validation = 1;
  2058. failrec->this_mirror = failed_mirror;
  2059. } else {
  2060. /*
  2061. * we're ready to fulfill a) and b) alongside. get a good copy
  2062. * of the failed sector and if we succeed, we have setup
  2063. * everything for repair_io_failure to do the rest for us.
  2064. */
  2065. if (failrec->in_validation) {
  2066. BUG_ON(failrec->this_mirror != failed_mirror);
  2067. failrec->in_validation = 0;
  2068. failrec->this_mirror = 0;
  2069. }
  2070. failrec->failed_mirror = failed_mirror;
  2071. failrec->this_mirror++;
  2072. if (failrec->this_mirror == failed_mirror)
  2073. failrec->this_mirror++;
  2074. }
  2075. if (failrec->this_mirror > num_copies) {
  2076. pr_debug("Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
  2077. num_copies, failrec->this_mirror, failed_mirror);
  2078. return 0;
  2079. }
  2080. return 1;
  2081. }
  2082. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  2083. struct io_failure_record *failrec,
  2084. struct page *page, int pg_offset, int icsum,
  2085. bio_end_io_t *endio_func, void *data)
  2086. {
  2087. struct bio *bio;
  2088. struct btrfs_io_bio *btrfs_failed_bio;
  2089. struct btrfs_io_bio *btrfs_bio;
  2090. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  2091. if (!bio)
  2092. return NULL;
  2093. bio->bi_end_io = endio_func;
  2094. bio->bi_iter.bi_sector = failrec->logical >> 9;
  2095. bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  2096. bio->bi_iter.bi_size = 0;
  2097. bio->bi_private = data;
  2098. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2099. if (btrfs_failed_bio->csum) {
  2100. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2101. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2102. btrfs_bio = btrfs_io_bio(bio);
  2103. btrfs_bio->csum = btrfs_bio->csum_inline;
  2104. icsum *= csum_size;
  2105. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
  2106. csum_size);
  2107. }
  2108. bio_add_page(bio, page, failrec->len, pg_offset);
  2109. return bio;
  2110. }
  2111. /*
  2112. * this is a generic handler for readpage errors (default
  2113. * readpage_io_failed_hook). if other copies exist, read those and write back
  2114. * good data to the failed position. does not investigate in remapping the
  2115. * failed extent elsewhere, hoping the device will be smart enough to do this as
  2116. * needed
  2117. */
  2118. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  2119. struct page *page, u64 start, u64 end,
  2120. int failed_mirror)
  2121. {
  2122. struct io_failure_record *failrec;
  2123. struct inode *inode = page->mapping->host;
  2124. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2125. struct bio *bio;
  2126. int read_mode;
  2127. int ret;
  2128. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  2129. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  2130. if (ret)
  2131. return ret;
  2132. ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
  2133. if (!ret) {
  2134. free_io_failure(inode, failrec);
  2135. return -EIO;
  2136. }
  2137. if (failed_bio->bi_vcnt > 1)
  2138. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  2139. else
  2140. read_mode = READ_SYNC;
  2141. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2142. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  2143. start - page_offset(page),
  2144. (int)phy_offset, failed_bio->bi_end_io,
  2145. NULL);
  2146. if (!bio) {
  2147. free_io_failure(inode, failrec);
  2148. return -EIO;
  2149. }
  2150. pr_debug("Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d\n",
  2151. read_mode, failrec->this_mirror, failrec->in_validation);
  2152. ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
  2153. failrec->this_mirror,
  2154. failrec->bio_flags, 0);
  2155. if (ret) {
  2156. free_io_failure(inode, failrec);
  2157. bio_put(bio);
  2158. }
  2159. return ret;
  2160. }
  2161. /* lots and lots of room for performance fixes in the end_bio funcs */
  2162. void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2163. {
  2164. int uptodate = (err == 0);
  2165. struct extent_io_tree *tree;
  2166. int ret = 0;
  2167. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2168. if (tree->ops && tree->ops->writepage_end_io_hook) {
  2169. ret = tree->ops->writepage_end_io_hook(page, start,
  2170. end, NULL, uptodate);
  2171. if (ret)
  2172. uptodate = 0;
  2173. }
  2174. if (!uptodate) {
  2175. ClearPageUptodate(page);
  2176. SetPageError(page);
  2177. ret = ret < 0 ? ret : -EIO;
  2178. mapping_set_error(page->mapping, ret);
  2179. }
  2180. }
  2181. /*
  2182. * after a writepage IO is done, we need to:
  2183. * clear the uptodate bits on error
  2184. * clear the writeback bits in the extent tree for this IO
  2185. * end_page_writeback if the page has no more pending IO
  2186. *
  2187. * Scheduling is not allowed, so the extent state tree is expected
  2188. * to have one and only one object corresponding to this IO.
  2189. */
  2190. static void end_bio_extent_writepage(struct bio *bio)
  2191. {
  2192. struct bio_vec *bvec;
  2193. u64 start;
  2194. u64 end;
  2195. int i;
  2196. bio_for_each_segment_all(bvec, bio, i) {
  2197. struct page *page = bvec->bv_page;
  2198. /* We always issue full-page reads, but if some block
  2199. * in a page fails to read, blk_update_request() will
  2200. * advance bv_offset and adjust bv_len to compensate.
  2201. * Print a warning for nonzero offsets, and an error
  2202. * if they don't add up to a full page. */
  2203. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2204. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2205. btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
  2206. "partial page write in btrfs with offset %u and length %u",
  2207. bvec->bv_offset, bvec->bv_len);
  2208. else
  2209. btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
  2210. "incomplete page write in btrfs with offset %u and "
  2211. "length %u",
  2212. bvec->bv_offset, bvec->bv_len);
  2213. }
  2214. start = page_offset(page);
  2215. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2216. end_extent_writepage(page, bio->bi_error, start, end);
  2217. end_page_writeback(page);
  2218. }
  2219. bio_put(bio);
  2220. }
  2221. static void
  2222. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2223. int uptodate)
  2224. {
  2225. struct extent_state *cached = NULL;
  2226. u64 end = start + len - 1;
  2227. if (uptodate && tree->track_uptodate)
  2228. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2229. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2230. }
  2231. /*
  2232. * after a readpage IO is done, we need to:
  2233. * clear the uptodate bits on error
  2234. * set the uptodate bits if things worked
  2235. * set the page up to date if all extents in the tree are uptodate
  2236. * clear the lock bit in the extent tree
  2237. * unlock the page if there are no other extents locked for it
  2238. *
  2239. * Scheduling is not allowed, so the extent state tree is expected
  2240. * to have one and only one object corresponding to this IO.
  2241. */
  2242. static void end_bio_extent_readpage(struct bio *bio)
  2243. {
  2244. struct bio_vec *bvec;
  2245. int uptodate = !bio->bi_error;
  2246. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2247. struct extent_io_tree *tree;
  2248. u64 offset = 0;
  2249. u64 start;
  2250. u64 end;
  2251. u64 len;
  2252. u64 extent_start = 0;
  2253. u64 extent_len = 0;
  2254. int mirror;
  2255. int ret;
  2256. int i;
  2257. bio_for_each_segment_all(bvec, bio, i) {
  2258. struct page *page = bvec->bv_page;
  2259. struct inode *inode = page->mapping->host;
  2260. pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
  2261. "mirror=%u\n", (u64)bio->bi_iter.bi_sector,
  2262. bio->bi_error, io_bio->mirror_num);
  2263. tree = &BTRFS_I(inode)->io_tree;
  2264. /* We always issue full-page reads, but if some block
  2265. * in a page fails to read, blk_update_request() will
  2266. * advance bv_offset and adjust bv_len to compensate.
  2267. * Print a warning for nonzero offsets, and an error
  2268. * if they don't add up to a full page. */
  2269. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2270. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2271. btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
  2272. "partial page read in btrfs with offset %u and length %u",
  2273. bvec->bv_offset, bvec->bv_len);
  2274. else
  2275. btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
  2276. "incomplete page read in btrfs with offset %u and "
  2277. "length %u",
  2278. bvec->bv_offset, bvec->bv_len);
  2279. }
  2280. start = page_offset(page);
  2281. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2282. len = bvec->bv_len;
  2283. mirror = io_bio->mirror_num;
  2284. if (likely(uptodate && tree->ops &&
  2285. tree->ops->readpage_end_io_hook)) {
  2286. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2287. page, start, end,
  2288. mirror);
  2289. if (ret)
  2290. uptodate = 0;
  2291. else
  2292. clean_io_failure(inode, start, page, 0);
  2293. }
  2294. if (likely(uptodate))
  2295. goto readpage_ok;
  2296. if (tree->ops && tree->ops->readpage_io_failed_hook) {
  2297. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2298. if (!ret && !bio->bi_error)
  2299. uptodate = 1;
  2300. } else {
  2301. /*
  2302. * The generic bio_readpage_error handles errors the
  2303. * following way: If possible, new read requests are
  2304. * created and submitted and will end up in
  2305. * end_bio_extent_readpage as well (if we're lucky, not
  2306. * in the !uptodate case). In that case it returns 0 and
  2307. * we just go on with the next page in our bio. If it
  2308. * can't handle the error it will return -EIO and we
  2309. * remain responsible for that page.
  2310. */
  2311. ret = bio_readpage_error(bio, offset, page, start, end,
  2312. mirror);
  2313. if (ret == 0) {
  2314. uptodate = !bio->bi_error;
  2315. offset += len;
  2316. continue;
  2317. }
  2318. }
  2319. readpage_ok:
  2320. if (likely(uptodate)) {
  2321. loff_t i_size = i_size_read(inode);
  2322. pgoff_t end_index = i_size >> PAGE_SHIFT;
  2323. unsigned off;
  2324. /* Zero out the end if this page straddles i_size */
  2325. off = i_size & (PAGE_SIZE-1);
  2326. if (page->index == end_index && off)
  2327. zero_user_segment(page, off, PAGE_SIZE);
  2328. SetPageUptodate(page);
  2329. } else {
  2330. ClearPageUptodate(page);
  2331. SetPageError(page);
  2332. }
  2333. unlock_page(page);
  2334. offset += len;
  2335. if (unlikely(!uptodate)) {
  2336. if (extent_len) {
  2337. endio_readpage_release_extent(tree,
  2338. extent_start,
  2339. extent_len, 1);
  2340. extent_start = 0;
  2341. extent_len = 0;
  2342. }
  2343. endio_readpage_release_extent(tree, start,
  2344. end - start + 1, 0);
  2345. } else if (!extent_len) {
  2346. extent_start = start;
  2347. extent_len = end + 1 - start;
  2348. } else if (extent_start + extent_len == start) {
  2349. extent_len += end + 1 - start;
  2350. } else {
  2351. endio_readpage_release_extent(tree, extent_start,
  2352. extent_len, uptodate);
  2353. extent_start = start;
  2354. extent_len = end + 1 - start;
  2355. }
  2356. }
  2357. if (extent_len)
  2358. endio_readpage_release_extent(tree, extent_start, extent_len,
  2359. uptodate);
  2360. if (io_bio->end_io)
  2361. io_bio->end_io(io_bio, bio->bi_error);
  2362. bio_put(bio);
  2363. }
  2364. /*
  2365. * this allocates from the btrfs_bioset. We're returning a bio right now
  2366. * but you can call btrfs_io_bio for the appropriate container_of magic
  2367. */
  2368. struct bio *
  2369. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2370. gfp_t gfp_flags)
  2371. {
  2372. struct btrfs_io_bio *btrfs_bio;
  2373. struct bio *bio;
  2374. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2375. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2376. while (!bio && (nr_vecs /= 2)) {
  2377. bio = bio_alloc_bioset(gfp_flags,
  2378. nr_vecs, btrfs_bioset);
  2379. }
  2380. }
  2381. if (bio) {
  2382. bio->bi_bdev = bdev;
  2383. bio->bi_iter.bi_sector = first_sector;
  2384. btrfs_bio = btrfs_io_bio(bio);
  2385. btrfs_bio->csum = NULL;
  2386. btrfs_bio->csum_allocated = NULL;
  2387. btrfs_bio->end_io = NULL;
  2388. }
  2389. return bio;
  2390. }
  2391. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2392. {
  2393. struct btrfs_io_bio *btrfs_bio;
  2394. struct bio *new;
  2395. new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
  2396. if (new) {
  2397. btrfs_bio = btrfs_io_bio(new);
  2398. btrfs_bio->csum = NULL;
  2399. btrfs_bio->csum_allocated = NULL;
  2400. btrfs_bio->end_io = NULL;
  2401. #ifdef CONFIG_BLK_CGROUP
  2402. /* FIXME, put this into bio_clone_bioset */
  2403. if (bio->bi_css)
  2404. bio_associate_blkcg(new, bio->bi_css);
  2405. #endif
  2406. }
  2407. return new;
  2408. }
  2409. /* this also allocates from the btrfs_bioset */
  2410. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2411. {
  2412. struct btrfs_io_bio *btrfs_bio;
  2413. struct bio *bio;
  2414. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2415. if (bio) {
  2416. btrfs_bio = btrfs_io_bio(bio);
  2417. btrfs_bio->csum = NULL;
  2418. btrfs_bio->csum_allocated = NULL;
  2419. btrfs_bio->end_io = NULL;
  2420. }
  2421. return bio;
  2422. }
  2423. static int __must_check submit_one_bio(int rw, struct bio *bio,
  2424. int mirror_num, unsigned long bio_flags)
  2425. {
  2426. int ret = 0;
  2427. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2428. struct page *page = bvec->bv_page;
  2429. struct extent_io_tree *tree = bio->bi_private;
  2430. u64 start;
  2431. start = page_offset(page) + bvec->bv_offset;
  2432. bio->bi_private = NULL;
  2433. bio_get(bio);
  2434. if (tree->ops && tree->ops->submit_bio_hook)
  2435. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2436. mirror_num, bio_flags, start);
  2437. else
  2438. btrfsic_submit_bio(rw, bio);
  2439. bio_put(bio);
  2440. return ret;
  2441. }
  2442. static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
  2443. unsigned long offset, size_t size, struct bio *bio,
  2444. unsigned long bio_flags)
  2445. {
  2446. int ret = 0;
  2447. if (tree->ops && tree->ops->merge_bio_hook)
  2448. ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
  2449. bio_flags);
  2450. BUG_ON(ret < 0);
  2451. return ret;
  2452. }
  2453. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2454. struct writeback_control *wbc,
  2455. struct page *page, sector_t sector,
  2456. size_t size, unsigned long offset,
  2457. struct block_device *bdev,
  2458. struct bio **bio_ret,
  2459. unsigned long max_pages,
  2460. bio_end_io_t end_io_func,
  2461. int mirror_num,
  2462. unsigned long prev_bio_flags,
  2463. unsigned long bio_flags,
  2464. bool force_bio_submit)
  2465. {
  2466. int ret = 0;
  2467. struct bio *bio;
  2468. int contig = 0;
  2469. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2470. size_t page_size = min_t(size_t, size, PAGE_SIZE);
  2471. if (bio_ret && *bio_ret) {
  2472. bio = *bio_ret;
  2473. if (old_compressed)
  2474. contig = bio->bi_iter.bi_sector == sector;
  2475. else
  2476. contig = bio_end_sector(bio) == sector;
  2477. if (prev_bio_flags != bio_flags || !contig ||
  2478. force_bio_submit ||
  2479. merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
  2480. bio_add_page(bio, page, page_size, offset) < page_size) {
  2481. ret = submit_one_bio(rw, bio, mirror_num,
  2482. prev_bio_flags);
  2483. if (ret < 0) {
  2484. *bio_ret = NULL;
  2485. return ret;
  2486. }
  2487. bio = NULL;
  2488. } else {
  2489. if (wbc)
  2490. wbc_account_io(wbc, page, page_size);
  2491. return 0;
  2492. }
  2493. }
  2494. bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
  2495. GFP_NOFS | __GFP_HIGH);
  2496. if (!bio)
  2497. return -ENOMEM;
  2498. bio_add_page(bio, page, page_size, offset);
  2499. bio->bi_end_io = end_io_func;
  2500. bio->bi_private = tree;
  2501. if (wbc) {
  2502. wbc_init_bio(wbc, bio);
  2503. wbc_account_io(wbc, page, page_size);
  2504. }
  2505. if (bio_ret)
  2506. *bio_ret = bio;
  2507. else
  2508. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2509. return ret;
  2510. }
  2511. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2512. struct page *page)
  2513. {
  2514. if (!PagePrivate(page)) {
  2515. SetPagePrivate(page);
  2516. get_page(page);
  2517. set_page_private(page, (unsigned long)eb);
  2518. } else {
  2519. WARN_ON(page->private != (unsigned long)eb);
  2520. }
  2521. }
  2522. void set_page_extent_mapped(struct page *page)
  2523. {
  2524. if (!PagePrivate(page)) {
  2525. SetPagePrivate(page);
  2526. get_page(page);
  2527. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2528. }
  2529. }
  2530. static struct extent_map *
  2531. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2532. u64 start, u64 len, get_extent_t *get_extent,
  2533. struct extent_map **em_cached)
  2534. {
  2535. struct extent_map *em;
  2536. if (em_cached && *em_cached) {
  2537. em = *em_cached;
  2538. if (extent_map_in_tree(em) && start >= em->start &&
  2539. start < extent_map_end(em)) {
  2540. atomic_inc(&em->refs);
  2541. return em;
  2542. }
  2543. free_extent_map(em);
  2544. *em_cached = NULL;
  2545. }
  2546. em = get_extent(inode, page, pg_offset, start, len, 0);
  2547. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2548. BUG_ON(*em_cached);
  2549. atomic_inc(&em->refs);
  2550. *em_cached = em;
  2551. }
  2552. return em;
  2553. }
  2554. /*
  2555. * basic readpage implementation. Locked extent state structs are inserted
  2556. * into the tree that are removed when the IO is done (by the end_io
  2557. * handlers)
  2558. * XXX JDM: This needs looking at to ensure proper page locking
  2559. */
  2560. static int __do_readpage(struct extent_io_tree *tree,
  2561. struct page *page,
  2562. get_extent_t *get_extent,
  2563. struct extent_map **em_cached,
  2564. struct bio **bio, int mirror_num,
  2565. unsigned long *bio_flags, int rw,
  2566. u64 *prev_em_start)
  2567. {
  2568. struct inode *inode = page->mapping->host;
  2569. u64 start = page_offset(page);
  2570. u64 page_end = start + PAGE_SIZE - 1;
  2571. u64 end;
  2572. u64 cur = start;
  2573. u64 extent_offset;
  2574. u64 last_byte = i_size_read(inode);
  2575. u64 block_start;
  2576. u64 cur_end;
  2577. sector_t sector;
  2578. struct extent_map *em;
  2579. struct block_device *bdev;
  2580. int ret;
  2581. int nr = 0;
  2582. size_t pg_offset = 0;
  2583. size_t iosize;
  2584. size_t disk_io_size;
  2585. size_t blocksize = inode->i_sb->s_blocksize;
  2586. unsigned long this_bio_flag = 0;
  2587. set_page_extent_mapped(page);
  2588. end = page_end;
  2589. if (!PageUptodate(page)) {
  2590. if (cleancache_get_page(page) == 0) {
  2591. BUG_ON(blocksize != PAGE_SIZE);
  2592. unlock_extent(tree, start, end);
  2593. goto out;
  2594. }
  2595. }
  2596. if (page->index == last_byte >> PAGE_SHIFT) {
  2597. char *userpage;
  2598. size_t zero_offset = last_byte & (PAGE_SIZE - 1);
  2599. if (zero_offset) {
  2600. iosize = PAGE_SIZE - zero_offset;
  2601. userpage = kmap_atomic(page);
  2602. memset(userpage + zero_offset, 0, iosize);
  2603. flush_dcache_page(page);
  2604. kunmap_atomic(userpage);
  2605. }
  2606. }
  2607. while (cur <= end) {
  2608. unsigned long pnr = (last_byte >> PAGE_SHIFT) + 1;
  2609. bool force_bio_submit = false;
  2610. if (cur >= last_byte) {
  2611. char *userpage;
  2612. struct extent_state *cached = NULL;
  2613. iosize = PAGE_SIZE - pg_offset;
  2614. userpage = kmap_atomic(page);
  2615. memset(userpage + pg_offset, 0, iosize);
  2616. flush_dcache_page(page);
  2617. kunmap_atomic(userpage);
  2618. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2619. &cached, GFP_NOFS);
  2620. unlock_extent_cached(tree, cur,
  2621. cur + iosize - 1,
  2622. &cached, GFP_NOFS);
  2623. break;
  2624. }
  2625. em = __get_extent_map(inode, page, pg_offset, cur,
  2626. end - cur + 1, get_extent, em_cached);
  2627. if (IS_ERR_OR_NULL(em)) {
  2628. SetPageError(page);
  2629. unlock_extent(tree, cur, end);
  2630. break;
  2631. }
  2632. extent_offset = cur - em->start;
  2633. BUG_ON(extent_map_end(em) <= cur);
  2634. BUG_ON(end < cur);
  2635. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2636. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2637. extent_set_compress_type(&this_bio_flag,
  2638. em->compress_type);
  2639. }
  2640. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2641. cur_end = min(extent_map_end(em) - 1, end);
  2642. iosize = ALIGN(iosize, blocksize);
  2643. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2644. disk_io_size = em->block_len;
  2645. sector = em->block_start >> 9;
  2646. } else {
  2647. sector = (em->block_start + extent_offset) >> 9;
  2648. disk_io_size = iosize;
  2649. }
  2650. bdev = em->bdev;
  2651. block_start = em->block_start;
  2652. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2653. block_start = EXTENT_MAP_HOLE;
  2654. /*
  2655. * If we have a file range that points to a compressed extent
  2656. * and it's followed by a consecutive file range that points to
  2657. * to the same compressed extent (possibly with a different
  2658. * offset and/or length, so it either points to the whole extent
  2659. * or only part of it), we must make sure we do not submit a
  2660. * single bio to populate the pages for the 2 ranges because
  2661. * this makes the compressed extent read zero out the pages
  2662. * belonging to the 2nd range. Imagine the following scenario:
  2663. *
  2664. * File layout
  2665. * [0 - 8K] [8K - 24K]
  2666. * | |
  2667. * | |
  2668. * points to extent X, points to extent X,
  2669. * offset 4K, length of 8K offset 0, length 16K
  2670. *
  2671. * [extent X, compressed length = 4K uncompressed length = 16K]
  2672. *
  2673. * If the bio to read the compressed extent covers both ranges,
  2674. * it will decompress extent X into the pages belonging to the
  2675. * first range and then it will stop, zeroing out the remaining
  2676. * pages that belong to the other range that points to extent X.
  2677. * So here we make sure we submit 2 bios, one for the first
  2678. * range and another one for the third range. Both will target
  2679. * the same physical extent from disk, but we can't currently
  2680. * make the compressed bio endio callback populate the pages
  2681. * for both ranges because each compressed bio is tightly
  2682. * coupled with a single extent map, and each range can have
  2683. * an extent map with a different offset value relative to the
  2684. * uncompressed data of our extent and different lengths. This
  2685. * is a corner case so we prioritize correctness over
  2686. * non-optimal behavior (submitting 2 bios for the same extent).
  2687. */
  2688. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
  2689. prev_em_start && *prev_em_start != (u64)-1 &&
  2690. *prev_em_start != em->orig_start)
  2691. force_bio_submit = true;
  2692. if (prev_em_start)
  2693. *prev_em_start = em->orig_start;
  2694. free_extent_map(em);
  2695. em = NULL;
  2696. /* we've found a hole, just zero and go on */
  2697. if (block_start == EXTENT_MAP_HOLE) {
  2698. char *userpage;
  2699. struct extent_state *cached = NULL;
  2700. userpage = kmap_atomic(page);
  2701. memset(userpage + pg_offset, 0, iosize);
  2702. flush_dcache_page(page);
  2703. kunmap_atomic(userpage);
  2704. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2705. &cached, GFP_NOFS);
  2706. unlock_extent_cached(tree, cur,
  2707. cur + iosize - 1,
  2708. &cached, GFP_NOFS);
  2709. cur = cur + iosize;
  2710. pg_offset += iosize;
  2711. continue;
  2712. }
  2713. /* the get_extent function already copied into the page */
  2714. if (test_range_bit(tree, cur, cur_end,
  2715. EXTENT_UPTODATE, 1, NULL)) {
  2716. check_page_uptodate(tree, page);
  2717. unlock_extent(tree, cur, cur + iosize - 1);
  2718. cur = cur + iosize;
  2719. pg_offset += iosize;
  2720. continue;
  2721. }
  2722. /* we have an inline extent but it didn't get marked up
  2723. * to date. Error out
  2724. */
  2725. if (block_start == EXTENT_MAP_INLINE) {
  2726. SetPageError(page);
  2727. unlock_extent(tree, cur, cur + iosize - 1);
  2728. cur = cur + iosize;
  2729. pg_offset += iosize;
  2730. continue;
  2731. }
  2732. pnr -= page->index;
  2733. ret = submit_extent_page(rw, tree, NULL, page,
  2734. sector, disk_io_size, pg_offset,
  2735. bdev, bio, pnr,
  2736. end_bio_extent_readpage, mirror_num,
  2737. *bio_flags,
  2738. this_bio_flag,
  2739. force_bio_submit);
  2740. if (!ret) {
  2741. nr++;
  2742. *bio_flags = this_bio_flag;
  2743. } else {
  2744. SetPageError(page);
  2745. unlock_extent(tree, cur, cur + iosize - 1);
  2746. }
  2747. cur = cur + iosize;
  2748. pg_offset += iosize;
  2749. }
  2750. out:
  2751. if (!nr) {
  2752. if (!PageError(page))
  2753. SetPageUptodate(page);
  2754. unlock_page(page);
  2755. }
  2756. return 0;
  2757. }
  2758. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2759. struct page *pages[], int nr_pages,
  2760. u64 start, u64 end,
  2761. get_extent_t *get_extent,
  2762. struct extent_map **em_cached,
  2763. struct bio **bio, int mirror_num,
  2764. unsigned long *bio_flags, int rw,
  2765. u64 *prev_em_start)
  2766. {
  2767. struct inode *inode;
  2768. struct btrfs_ordered_extent *ordered;
  2769. int index;
  2770. inode = pages[0]->mapping->host;
  2771. while (1) {
  2772. lock_extent(tree, start, end);
  2773. ordered = btrfs_lookup_ordered_range(inode, start,
  2774. end - start + 1);
  2775. if (!ordered)
  2776. break;
  2777. unlock_extent(tree, start, end);
  2778. btrfs_start_ordered_extent(inode, ordered, 1);
  2779. btrfs_put_ordered_extent(ordered);
  2780. }
  2781. for (index = 0; index < nr_pages; index++) {
  2782. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2783. mirror_num, bio_flags, rw, prev_em_start);
  2784. put_page(pages[index]);
  2785. }
  2786. }
  2787. static void __extent_readpages(struct extent_io_tree *tree,
  2788. struct page *pages[],
  2789. int nr_pages, get_extent_t *get_extent,
  2790. struct extent_map **em_cached,
  2791. struct bio **bio, int mirror_num,
  2792. unsigned long *bio_flags, int rw,
  2793. u64 *prev_em_start)
  2794. {
  2795. u64 start = 0;
  2796. u64 end = 0;
  2797. u64 page_start;
  2798. int index;
  2799. int first_index = 0;
  2800. for (index = 0; index < nr_pages; index++) {
  2801. page_start = page_offset(pages[index]);
  2802. if (!end) {
  2803. start = page_start;
  2804. end = start + PAGE_SIZE - 1;
  2805. first_index = index;
  2806. } else if (end + 1 == page_start) {
  2807. end += PAGE_SIZE;
  2808. } else {
  2809. __do_contiguous_readpages(tree, &pages[first_index],
  2810. index - first_index, start,
  2811. end, get_extent, em_cached,
  2812. bio, mirror_num, bio_flags,
  2813. rw, prev_em_start);
  2814. start = page_start;
  2815. end = start + PAGE_SIZE - 1;
  2816. first_index = index;
  2817. }
  2818. }
  2819. if (end)
  2820. __do_contiguous_readpages(tree, &pages[first_index],
  2821. index - first_index, start,
  2822. end, get_extent, em_cached, bio,
  2823. mirror_num, bio_flags, rw,
  2824. prev_em_start);
  2825. }
  2826. static int __extent_read_full_page(struct extent_io_tree *tree,
  2827. struct page *page,
  2828. get_extent_t *get_extent,
  2829. struct bio **bio, int mirror_num,
  2830. unsigned long *bio_flags, int rw)
  2831. {
  2832. struct inode *inode = page->mapping->host;
  2833. struct btrfs_ordered_extent *ordered;
  2834. u64 start = page_offset(page);
  2835. u64 end = start + PAGE_SIZE - 1;
  2836. int ret;
  2837. while (1) {
  2838. lock_extent(tree, start, end);
  2839. ordered = btrfs_lookup_ordered_range(inode, start,
  2840. PAGE_SIZE);
  2841. if (!ordered)
  2842. break;
  2843. unlock_extent(tree, start, end);
  2844. btrfs_start_ordered_extent(inode, ordered, 1);
  2845. btrfs_put_ordered_extent(ordered);
  2846. }
  2847. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2848. bio_flags, rw, NULL);
  2849. return ret;
  2850. }
  2851. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2852. get_extent_t *get_extent, int mirror_num)
  2853. {
  2854. struct bio *bio = NULL;
  2855. unsigned long bio_flags = 0;
  2856. int ret;
  2857. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2858. &bio_flags, READ);
  2859. if (bio)
  2860. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2861. return ret;
  2862. }
  2863. static void update_nr_written(struct page *page, struct writeback_control *wbc,
  2864. unsigned long nr_written)
  2865. {
  2866. wbc->nr_to_write -= nr_written;
  2867. }
  2868. /*
  2869. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2870. *
  2871. * This returns 1 if our fill_delalloc function did all the work required
  2872. * to write the page (copy into inline extent). In this case the IO has
  2873. * been started and the page is already unlocked.
  2874. *
  2875. * This returns 0 if all went well (page still locked)
  2876. * This returns < 0 if there were errors (page still locked)
  2877. */
  2878. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2879. struct page *page, struct writeback_control *wbc,
  2880. struct extent_page_data *epd,
  2881. u64 delalloc_start,
  2882. unsigned long *nr_written)
  2883. {
  2884. struct extent_io_tree *tree = epd->tree;
  2885. u64 page_end = delalloc_start + PAGE_SIZE - 1;
  2886. u64 nr_delalloc;
  2887. u64 delalloc_to_write = 0;
  2888. u64 delalloc_end = 0;
  2889. int ret;
  2890. int page_started = 0;
  2891. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2892. return 0;
  2893. while (delalloc_end < page_end) {
  2894. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2895. page,
  2896. &delalloc_start,
  2897. &delalloc_end,
  2898. BTRFS_MAX_EXTENT_SIZE);
  2899. if (nr_delalloc == 0) {
  2900. delalloc_start = delalloc_end + 1;
  2901. continue;
  2902. }
  2903. ret = tree->ops->fill_delalloc(inode, page,
  2904. delalloc_start,
  2905. delalloc_end,
  2906. &page_started,
  2907. nr_written);
  2908. /* File system has been set read-only */
  2909. if (ret) {
  2910. SetPageError(page);
  2911. /* fill_delalloc should be return < 0 for error
  2912. * but just in case, we use > 0 here meaning the
  2913. * IO is started, so we don't want to return > 0
  2914. * unless things are going well.
  2915. */
  2916. ret = ret < 0 ? ret : -EIO;
  2917. goto done;
  2918. }
  2919. /*
  2920. * delalloc_end is already one less than the total length, so
  2921. * we don't subtract one from PAGE_SIZE
  2922. */
  2923. delalloc_to_write += (delalloc_end - delalloc_start +
  2924. PAGE_SIZE) >> PAGE_SHIFT;
  2925. delalloc_start = delalloc_end + 1;
  2926. }
  2927. if (wbc->nr_to_write < delalloc_to_write) {
  2928. int thresh = 8192;
  2929. if (delalloc_to_write < thresh * 2)
  2930. thresh = delalloc_to_write;
  2931. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2932. thresh);
  2933. }
  2934. /* did the fill delalloc function already unlock and start
  2935. * the IO?
  2936. */
  2937. if (page_started) {
  2938. /*
  2939. * we've unlocked the page, so we can't update
  2940. * the mapping's writeback index, just update
  2941. * nr_to_write.
  2942. */
  2943. wbc->nr_to_write -= *nr_written;
  2944. return 1;
  2945. }
  2946. ret = 0;
  2947. done:
  2948. return ret;
  2949. }
  2950. /*
  2951. * helper for __extent_writepage. This calls the writepage start hooks,
  2952. * and does the loop to map the page into extents and bios.
  2953. *
  2954. * We return 1 if the IO is started and the page is unlocked,
  2955. * 0 if all went well (page still locked)
  2956. * < 0 if there were errors (page still locked)
  2957. */
  2958. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2959. struct page *page,
  2960. struct writeback_control *wbc,
  2961. struct extent_page_data *epd,
  2962. loff_t i_size,
  2963. unsigned long nr_written,
  2964. int write_flags, int *nr_ret)
  2965. {
  2966. struct extent_io_tree *tree = epd->tree;
  2967. u64 start = page_offset(page);
  2968. u64 page_end = start + PAGE_SIZE - 1;
  2969. u64 end;
  2970. u64 cur = start;
  2971. u64 extent_offset;
  2972. u64 block_start;
  2973. u64 iosize;
  2974. sector_t sector;
  2975. struct extent_state *cached_state = NULL;
  2976. struct extent_map *em;
  2977. struct block_device *bdev;
  2978. size_t pg_offset = 0;
  2979. size_t blocksize;
  2980. int ret = 0;
  2981. int nr = 0;
  2982. bool compressed;
  2983. if (tree->ops && tree->ops->writepage_start_hook) {
  2984. ret = tree->ops->writepage_start_hook(page, start,
  2985. page_end);
  2986. if (ret) {
  2987. /* Fixup worker will requeue */
  2988. if (ret == -EBUSY)
  2989. wbc->pages_skipped++;
  2990. else
  2991. redirty_page_for_writepage(wbc, page);
  2992. update_nr_written(page, wbc, nr_written);
  2993. unlock_page(page);
  2994. ret = 1;
  2995. goto done_unlocked;
  2996. }
  2997. }
  2998. /*
  2999. * we don't want to touch the inode after unlocking the page,
  3000. * so we update the mapping writeback index now
  3001. */
  3002. update_nr_written(page, wbc, nr_written + 1);
  3003. end = page_end;
  3004. if (i_size <= start) {
  3005. if (tree->ops && tree->ops->writepage_end_io_hook)
  3006. tree->ops->writepage_end_io_hook(page, start,
  3007. page_end, NULL, 1);
  3008. goto done;
  3009. }
  3010. blocksize = inode->i_sb->s_blocksize;
  3011. while (cur <= end) {
  3012. u64 em_end;
  3013. unsigned long max_nr;
  3014. if (cur >= i_size) {
  3015. if (tree->ops && tree->ops->writepage_end_io_hook)
  3016. tree->ops->writepage_end_io_hook(page, cur,
  3017. page_end, NULL, 1);
  3018. break;
  3019. }
  3020. em = epd->get_extent(inode, page, pg_offset, cur,
  3021. end - cur + 1, 1);
  3022. if (IS_ERR_OR_NULL(em)) {
  3023. SetPageError(page);
  3024. ret = PTR_ERR_OR_ZERO(em);
  3025. break;
  3026. }
  3027. extent_offset = cur - em->start;
  3028. em_end = extent_map_end(em);
  3029. BUG_ON(em_end <= cur);
  3030. BUG_ON(end < cur);
  3031. iosize = min(em_end - cur, end - cur + 1);
  3032. iosize = ALIGN(iosize, blocksize);
  3033. sector = (em->block_start + extent_offset) >> 9;
  3034. bdev = em->bdev;
  3035. block_start = em->block_start;
  3036. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3037. free_extent_map(em);
  3038. em = NULL;
  3039. /*
  3040. * compressed and inline extents are written through other
  3041. * paths in the FS
  3042. */
  3043. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3044. block_start == EXTENT_MAP_INLINE) {
  3045. /*
  3046. * end_io notification does not happen here for
  3047. * compressed extents
  3048. */
  3049. if (!compressed && tree->ops &&
  3050. tree->ops->writepage_end_io_hook)
  3051. tree->ops->writepage_end_io_hook(page, cur,
  3052. cur + iosize - 1,
  3053. NULL, 1);
  3054. else if (compressed) {
  3055. /* we don't want to end_page_writeback on
  3056. * a compressed extent. this happens
  3057. * elsewhere
  3058. */
  3059. nr++;
  3060. }
  3061. cur += iosize;
  3062. pg_offset += iosize;
  3063. continue;
  3064. }
  3065. max_nr = (i_size >> PAGE_SHIFT) + 1;
  3066. set_range_writeback(tree, cur, cur + iosize - 1);
  3067. if (!PageWriteback(page)) {
  3068. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3069. "page %lu not writeback, cur %llu end %llu",
  3070. page->index, cur, end);
  3071. }
  3072. ret = submit_extent_page(write_flags, tree, wbc, page,
  3073. sector, iosize, pg_offset,
  3074. bdev, &epd->bio, max_nr,
  3075. end_bio_extent_writepage,
  3076. 0, 0, 0, false);
  3077. if (ret)
  3078. SetPageError(page);
  3079. cur = cur + iosize;
  3080. pg_offset += iosize;
  3081. nr++;
  3082. }
  3083. done:
  3084. *nr_ret = nr;
  3085. done_unlocked:
  3086. /* drop our reference on any cached states */
  3087. free_extent_state(cached_state);
  3088. return ret;
  3089. }
  3090. /*
  3091. * the writepage semantics are similar to regular writepage. extent
  3092. * records are inserted to lock ranges in the tree, and as dirty areas
  3093. * are found, they are marked writeback. Then the lock bits are removed
  3094. * and the end_io handler clears the writeback ranges
  3095. */
  3096. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3097. void *data)
  3098. {
  3099. struct inode *inode = page->mapping->host;
  3100. struct extent_page_data *epd = data;
  3101. u64 start = page_offset(page);
  3102. u64 page_end = start + PAGE_SIZE - 1;
  3103. int ret;
  3104. int nr = 0;
  3105. size_t pg_offset = 0;
  3106. loff_t i_size = i_size_read(inode);
  3107. unsigned long end_index = i_size >> PAGE_SHIFT;
  3108. int write_flags;
  3109. unsigned long nr_written = 0;
  3110. if (wbc->sync_mode == WB_SYNC_ALL)
  3111. write_flags = WRITE_SYNC;
  3112. else
  3113. write_flags = WRITE;
  3114. trace___extent_writepage(page, inode, wbc);
  3115. WARN_ON(!PageLocked(page));
  3116. ClearPageError(page);
  3117. pg_offset = i_size & (PAGE_SIZE - 1);
  3118. if (page->index > end_index ||
  3119. (page->index == end_index && !pg_offset)) {
  3120. page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
  3121. unlock_page(page);
  3122. return 0;
  3123. }
  3124. if (page->index == end_index) {
  3125. char *userpage;
  3126. userpage = kmap_atomic(page);
  3127. memset(userpage + pg_offset, 0,
  3128. PAGE_SIZE - pg_offset);
  3129. kunmap_atomic(userpage);
  3130. flush_dcache_page(page);
  3131. }
  3132. pg_offset = 0;
  3133. set_page_extent_mapped(page);
  3134. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3135. if (ret == 1)
  3136. goto done_unlocked;
  3137. if (ret)
  3138. goto done;
  3139. ret = __extent_writepage_io(inode, page, wbc, epd,
  3140. i_size, nr_written, write_flags, &nr);
  3141. if (ret == 1)
  3142. goto done_unlocked;
  3143. done:
  3144. if (nr == 0) {
  3145. /* make sure the mapping tag for page dirty gets cleared */
  3146. set_page_writeback(page);
  3147. end_page_writeback(page);
  3148. }
  3149. if (PageError(page)) {
  3150. ret = ret < 0 ? ret : -EIO;
  3151. end_extent_writepage(page, ret, start, page_end);
  3152. }
  3153. unlock_page(page);
  3154. return ret;
  3155. done_unlocked:
  3156. return 0;
  3157. }
  3158. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3159. {
  3160. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3161. TASK_UNINTERRUPTIBLE);
  3162. }
  3163. static noinline_for_stack int
  3164. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3165. struct btrfs_fs_info *fs_info,
  3166. struct extent_page_data *epd)
  3167. {
  3168. unsigned long i, num_pages;
  3169. int flush = 0;
  3170. int ret = 0;
  3171. if (!btrfs_try_tree_write_lock(eb)) {
  3172. flush = 1;
  3173. flush_write_bio(epd);
  3174. btrfs_tree_lock(eb);
  3175. }
  3176. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3177. btrfs_tree_unlock(eb);
  3178. if (!epd->sync_io)
  3179. return 0;
  3180. if (!flush) {
  3181. flush_write_bio(epd);
  3182. flush = 1;
  3183. }
  3184. while (1) {
  3185. wait_on_extent_buffer_writeback(eb);
  3186. btrfs_tree_lock(eb);
  3187. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3188. break;
  3189. btrfs_tree_unlock(eb);
  3190. }
  3191. }
  3192. /*
  3193. * We need to do this to prevent races in people who check if the eb is
  3194. * under IO since we can end up having no IO bits set for a short period
  3195. * of time.
  3196. */
  3197. spin_lock(&eb->refs_lock);
  3198. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3199. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3200. spin_unlock(&eb->refs_lock);
  3201. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3202. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3203. -eb->len,
  3204. fs_info->dirty_metadata_batch);
  3205. ret = 1;
  3206. } else {
  3207. spin_unlock(&eb->refs_lock);
  3208. }
  3209. btrfs_tree_unlock(eb);
  3210. if (!ret)
  3211. return ret;
  3212. num_pages = num_extent_pages(eb->start, eb->len);
  3213. for (i = 0; i < num_pages; i++) {
  3214. struct page *p = eb->pages[i];
  3215. if (!trylock_page(p)) {
  3216. if (!flush) {
  3217. flush_write_bio(epd);
  3218. flush = 1;
  3219. }
  3220. lock_page(p);
  3221. }
  3222. }
  3223. return ret;
  3224. }
  3225. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3226. {
  3227. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3228. smp_mb__after_atomic();
  3229. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3230. }
  3231. static void set_btree_ioerr(struct page *page)
  3232. {
  3233. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3234. struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode);
  3235. SetPageError(page);
  3236. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3237. return;
  3238. /*
  3239. * If writeback for a btree extent that doesn't belong to a log tree
  3240. * failed, increment the counter transaction->eb_write_errors.
  3241. * We do this because while the transaction is running and before it's
  3242. * committing (when we call filemap_fdata[write|wait]_range against
  3243. * the btree inode), we might have
  3244. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3245. * returns an error or an error happens during writeback, when we're
  3246. * committing the transaction we wouldn't know about it, since the pages
  3247. * can be no longer dirty nor marked anymore for writeback (if a
  3248. * subsequent modification to the extent buffer didn't happen before the
  3249. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3250. * able to find the pages tagged with SetPageError at transaction
  3251. * commit time. So if this happens we must abort the transaction,
  3252. * otherwise we commit a super block with btree roots that point to
  3253. * btree nodes/leafs whose content on disk is invalid - either garbage
  3254. * or the content of some node/leaf from a past generation that got
  3255. * cowed or deleted and is no longer valid.
  3256. *
  3257. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3258. * not be enough - we need to distinguish between log tree extents vs
  3259. * non-log tree extents, and the next filemap_fdatawait_range() call
  3260. * will catch and clear such errors in the mapping - and that call might
  3261. * be from a log sync and not from a transaction commit. Also, checking
  3262. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3263. * not done and would not be reliable - the eb might have been released
  3264. * from memory and reading it back again means that flag would not be
  3265. * set (since it's a runtime flag, not persisted on disk).
  3266. *
  3267. * Using the flags below in the btree inode also makes us achieve the
  3268. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3269. * writeback for all dirty pages and before filemap_fdatawait_range()
  3270. * is called, the writeback for all dirty pages had already finished
  3271. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3272. * filemap_fdatawait_range() would return success, as it could not know
  3273. * that writeback errors happened (the pages were no longer tagged for
  3274. * writeback).
  3275. */
  3276. switch (eb->log_index) {
  3277. case -1:
  3278. set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags);
  3279. break;
  3280. case 0:
  3281. set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
  3282. break;
  3283. case 1:
  3284. set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
  3285. break;
  3286. default:
  3287. BUG(); /* unexpected, logic error */
  3288. }
  3289. }
  3290. static void end_bio_extent_buffer_writepage(struct bio *bio)
  3291. {
  3292. struct bio_vec *bvec;
  3293. struct extent_buffer *eb;
  3294. int i, done;
  3295. bio_for_each_segment_all(bvec, bio, i) {
  3296. struct page *page = bvec->bv_page;
  3297. eb = (struct extent_buffer *)page->private;
  3298. BUG_ON(!eb);
  3299. done = atomic_dec_and_test(&eb->io_pages);
  3300. if (bio->bi_error ||
  3301. test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3302. ClearPageUptodate(page);
  3303. set_btree_ioerr(page);
  3304. }
  3305. end_page_writeback(page);
  3306. if (!done)
  3307. continue;
  3308. end_extent_buffer_writeback(eb);
  3309. }
  3310. bio_put(bio);
  3311. }
  3312. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3313. struct btrfs_fs_info *fs_info,
  3314. struct writeback_control *wbc,
  3315. struct extent_page_data *epd)
  3316. {
  3317. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3318. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3319. u64 offset = eb->start;
  3320. unsigned long i, num_pages;
  3321. unsigned long bio_flags = 0;
  3322. int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
  3323. int ret = 0;
  3324. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3325. num_pages = num_extent_pages(eb->start, eb->len);
  3326. atomic_set(&eb->io_pages, num_pages);
  3327. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3328. bio_flags = EXTENT_BIO_TREE_LOG;
  3329. for (i = 0; i < num_pages; i++) {
  3330. struct page *p = eb->pages[i];
  3331. clear_page_dirty_for_io(p);
  3332. set_page_writeback(p);
  3333. ret = submit_extent_page(rw, tree, wbc, p, offset >> 9,
  3334. PAGE_SIZE, 0, bdev, &epd->bio,
  3335. -1, end_bio_extent_buffer_writepage,
  3336. 0, epd->bio_flags, bio_flags, false);
  3337. epd->bio_flags = bio_flags;
  3338. if (ret) {
  3339. set_btree_ioerr(p);
  3340. end_page_writeback(p);
  3341. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3342. end_extent_buffer_writeback(eb);
  3343. ret = -EIO;
  3344. break;
  3345. }
  3346. offset += PAGE_SIZE;
  3347. update_nr_written(p, wbc, 1);
  3348. unlock_page(p);
  3349. }
  3350. if (unlikely(ret)) {
  3351. for (; i < num_pages; i++) {
  3352. struct page *p = eb->pages[i];
  3353. clear_page_dirty_for_io(p);
  3354. unlock_page(p);
  3355. }
  3356. }
  3357. return ret;
  3358. }
  3359. int btree_write_cache_pages(struct address_space *mapping,
  3360. struct writeback_control *wbc)
  3361. {
  3362. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3363. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3364. struct extent_buffer *eb, *prev_eb = NULL;
  3365. struct extent_page_data epd = {
  3366. .bio = NULL,
  3367. .tree = tree,
  3368. .extent_locked = 0,
  3369. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3370. .bio_flags = 0,
  3371. };
  3372. int ret = 0;
  3373. int done = 0;
  3374. int nr_to_write_done = 0;
  3375. struct pagevec pvec;
  3376. int nr_pages;
  3377. pgoff_t index;
  3378. pgoff_t end; /* Inclusive */
  3379. int scanned = 0;
  3380. int tag;
  3381. pagevec_init(&pvec, 0);
  3382. if (wbc->range_cyclic) {
  3383. index = mapping->writeback_index; /* Start from prev offset */
  3384. end = -1;
  3385. } else {
  3386. index = wbc->range_start >> PAGE_SHIFT;
  3387. end = wbc->range_end >> PAGE_SHIFT;
  3388. scanned = 1;
  3389. }
  3390. if (wbc->sync_mode == WB_SYNC_ALL)
  3391. tag = PAGECACHE_TAG_TOWRITE;
  3392. else
  3393. tag = PAGECACHE_TAG_DIRTY;
  3394. retry:
  3395. if (wbc->sync_mode == WB_SYNC_ALL)
  3396. tag_pages_for_writeback(mapping, index, end);
  3397. while (!done && !nr_to_write_done && (index <= end) &&
  3398. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3399. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3400. unsigned i;
  3401. scanned = 1;
  3402. for (i = 0; i < nr_pages; i++) {
  3403. struct page *page = pvec.pages[i];
  3404. if (!PagePrivate(page))
  3405. continue;
  3406. if (!wbc->range_cyclic && page->index > end) {
  3407. done = 1;
  3408. break;
  3409. }
  3410. spin_lock(&mapping->private_lock);
  3411. if (!PagePrivate(page)) {
  3412. spin_unlock(&mapping->private_lock);
  3413. continue;
  3414. }
  3415. eb = (struct extent_buffer *)page->private;
  3416. /*
  3417. * Shouldn't happen and normally this would be a BUG_ON
  3418. * but no sense in crashing the users box for something
  3419. * we can survive anyway.
  3420. */
  3421. if (WARN_ON(!eb)) {
  3422. spin_unlock(&mapping->private_lock);
  3423. continue;
  3424. }
  3425. if (eb == prev_eb) {
  3426. spin_unlock(&mapping->private_lock);
  3427. continue;
  3428. }
  3429. ret = atomic_inc_not_zero(&eb->refs);
  3430. spin_unlock(&mapping->private_lock);
  3431. if (!ret)
  3432. continue;
  3433. prev_eb = eb;
  3434. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3435. if (!ret) {
  3436. free_extent_buffer(eb);
  3437. continue;
  3438. }
  3439. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3440. if (ret) {
  3441. done = 1;
  3442. free_extent_buffer(eb);
  3443. break;
  3444. }
  3445. free_extent_buffer(eb);
  3446. /*
  3447. * the filesystem may choose to bump up nr_to_write.
  3448. * We have to make sure to honor the new nr_to_write
  3449. * at any time
  3450. */
  3451. nr_to_write_done = wbc->nr_to_write <= 0;
  3452. }
  3453. pagevec_release(&pvec);
  3454. cond_resched();
  3455. }
  3456. if (!scanned && !done) {
  3457. /*
  3458. * We hit the last page and there is more work to be done: wrap
  3459. * back to the start of the file
  3460. */
  3461. scanned = 1;
  3462. index = 0;
  3463. goto retry;
  3464. }
  3465. flush_write_bio(&epd);
  3466. return ret;
  3467. }
  3468. /**
  3469. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3470. * @mapping: address space structure to write
  3471. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3472. * @writepage: function called for each page
  3473. * @data: data passed to writepage function
  3474. *
  3475. * If a page is already under I/O, write_cache_pages() skips it, even
  3476. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3477. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3478. * and msync() need to guarantee that all the data which was dirty at the time
  3479. * the call was made get new I/O started against them. If wbc->sync_mode is
  3480. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3481. * existing IO to complete.
  3482. */
  3483. static int extent_write_cache_pages(struct extent_io_tree *tree,
  3484. struct address_space *mapping,
  3485. struct writeback_control *wbc,
  3486. writepage_t writepage, void *data,
  3487. void (*flush_fn)(void *))
  3488. {
  3489. struct inode *inode = mapping->host;
  3490. int ret = 0;
  3491. int done = 0;
  3492. int nr_to_write_done = 0;
  3493. struct pagevec pvec;
  3494. int nr_pages;
  3495. pgoff_t index;
  3496. pgoff_t end; /* Inclusive */
  3497. pgoff_t done_index;
  3498. int range_whole = 0;
  3499. int scanned = 0;
  3500. int tag;
  3501. /*
  3502. * We have to hold onto the inode so that ordered extents can do their
  3503. * work when the IO finishes. The alternative to this is failing to add
  3504. * an ordered extent if the igrab() fails there and that is a huge pain
  3505. * to deal with, so instead just hold onto the inode throughout the
  3506. * writepages operation. If it fails here we are freeing up the inode
  3507. * anyway and we'd rather not waste our time writing out stuff that is
  3508. * going to be truncated anyway.
  3509. */
  3510. if (!igrab(inode))
  3511. return 0;
  3512. pagevec_init(&pvec, 0);
  3513. if (wbc->range_cyclic) {
  3514. index = mapping->writeback_index; /* Start from prev offset */
  3515. end = -1;
  3516. } else {
  3517. index = wbc->range_start >> PAGE_SHIFT;
  3518. end = wbc->range_end >> PAGE_SHIFT;
  3519. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  3520. range_whole = 1;
  3521. scanned = 1;
  3522. }
  3523. if (wbc->sync_mode == WB_SYNC_ALL)
  3524. tag = PAGECACHE_TAG_TOWRITE;
  3525. else
  3526. tag = PAGECACHE_TAG_DIRTY;
  3527. retry:
  3528. if (wbc->sync_mode == WB_SYNC_ALL)
  3529. tag_pages_for_writeback(mapping, index, end);
  3530. done_index = index;
  3531. while (!done && !nr_to_write_done && (index <= end) &&
  3532. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3533. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3534. unsigned i;
  3535. scanned = 1;
  3536. for (i = 0; i < nr_pages; i++) {
  3537. struct page *page = pvec.pages[i];
  3538. done_index = page->index;
  3539. /*
  3540. * At this point we hold neither mapping->tree_lock nor
  3541. * lock on the page itself: the page may be truncated or
  3542. * invalidated (changing page->mapping to NULL), or even
  3543. * swizzled back from swapper_space to tmpfs file
  3544. * mapping
  3545. */
  3546. if (!trylock_page(page)) {
  3547. flush_fn(data);
  3548. lock_page(page);
  3549. }
  3550. if (unlikely(page->mapping != mapping)) {
  3551. unlock_page(page);
  3552. continue;
  3553. }
  3554. if (!wbc->range_cyclic && page->index > end) {
  3555. done = 1;
  3556. unlock_page(page);
  3557. continue;
  3558. }
  3559. if (wbc->sync_mode != WB_SYNC_NONE) {
  3560. if (PageWriteback(page))
  3561. flush_fn(data);
  3562. wait_on_page_writeback(page);
  3563. }
  3564. if (PageWriteback(page) ||
  3565. !clear_page_dirty_for_io(page)) {
  3566. unlock_page(page);
  3567. continue;
  3568. }
  3569. ret = (*writepage)(page, wbc, data);
  3570. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3571. unlock_page(page);
  3572. ret = 0;
  3573. }
  3574. if (ret < 0) {
  3575. /*
  3576. * done_index is set past this page,
  3577. * so media errors will not choke
  3578. * background writeout for the entire
  3579. * file. This has consequences for
  3580. * range_cyclic semantics (ie. it may
  3581. * not be suitable for data integrity
  3582. * writeout).
  3583. */
  3584. done_index = page->index + 1;
  3585. done = 1;
  3586. break;
  3587. }
  3588. /*
  3589. * the filesystem may choose to bump up nr_to_write.
  3590. * We have to make sure to honor the new nr_to_write
  3591. * at any time
  3592. */
  3593. nr_to_write_done = wbc->nr_to_write <= 0;
  3594. }
  3595. pagevec_release(&pvec);
  3596. cond_resched();
  3597. }
  3598. if (!scanned && !done) {
  3599. /*
  3600. * We hit the last page and there is more work to be done: wrap
  3601. * back to the start of the file
  3602. */
  3603. scanned = 1;
  3604. index = 0;
  3605. goto retry;
  3606. }
  3607. if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
  3608. mapping->writeback_index = done_index;
  3609. btrfs_add_delayed_iput(inode);
  3610. return ret;
  3611. }
  3612. static void flush_epd_write_bio(struct extent_page_data *epd)
  3613. {
  3614. if (epd->bio) {
  3615. int rw = WRITE;
  3616. int ret;
  3617. if (epd->sync_io)
  3618. rw = WRITE_SYNC;
  3619. ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
  3620. BUG_ON(ret < 0); /* -ENOMEM */
  3621. epd->bio = NULL;
  3622. }
  3623. }
  3624. static noinline void flush_write_bio(void *data)
  3625. {
  3626. struct extent_page_data *epd = data;
  3627. flush_epd_write_bio(epd);
  3628. }
  3629. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3630. get_extent_t *get_extent,
  3631. struct writeback_control *wbc)
  3632. {
  3633. int ret;
  3634. struct extent_page_data epd = {
  3635. .bio = NULL,
  3636. .tree = tree,
  3637. .get_extent = get_extent,
  3638. .extent_locked = 0,
  3639. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3640. .bio_flags = 0,
  3641. };
  3642. ret = __extent_writepage(page, wbc, &epd);
  3643. flush_epd_write_bio(&epd);
  3644. return ret;
  3645. }
  3646. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3647. u64 start, u64 end, get_extent_t *get_extent,
  3648. int mode)
  3649. {
  3650. int ret = 0;
  3651. struct address_space *mapping = inode->i_mapping;
  3652. struct page *page;
  3653. unsigned long nr_pages = (end - start + PAGE_SIZE) >>
  3654. PAGE_SHIFT;
  3655. struct extent_page_data epd = {
  3656. .bio = NULL,
  3657. .tree = tree,
  3658. .get_extent = get_extent,
  3659. .extent_locked = 1,
  3660. .sync_io = mode == WB_SYNC_ALL,
  3661. .bio_flags = 0,
  3662. };
  3663. struct writeback_control wbc_writepages = {
  3664. .sync_mode = mode,
  3665. .nr_to_write = nr_pages * 2,
  3666. .range_start = start,
  3667. .range_end = end + 1,
  3668. };
  3669. while (start <= end) {
  3670. page = find_get_page(mapping, start >> PAGE_SHIFT);
  3671. if (clear_page_dirty_for_io(page))
  3672. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3673. else {
  3674. if (tree->ops && tree->ops->writepage_end_io_hook)
  3675. tree->ops->writepage_end_io_hook(page, start,
  3676. start + PAGE_SIZE - 1,
  3677. NULL, 1);
  3678. unlock_page(page);
  3679. }
  3680. put_page(page);
  3681. start += PAGE_SIZE;
  3682. }
  3683. flush_epd_write_bio(&epd);
  3684. return ret;
  3685. }
  3686. int extent_writepages(struct extent_io_tree *tree,
  3687. struct address_space *mapping,
  3688. get_extent_t *get_extent,
  3689. struct writeback_control *wbc)
  3690. {
  3691. int ret = 0;
  3692. struct extent_page_data epd = {
  3693. .bio = NULL,
  3694. .tree = tree,
  3695. .get_extent = get_extent,
  3696. .extent_locked = 0,
  3697. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3698. .bio_flags = 0,
  3699. };
  3700. ret = extent_write_cache_pages(tree, mapping, wbc,
  3701. __extent_writepage, &epd,
  3702. flush_write_bio);
  3703. flush_epd_write_bio(&epd);
  3704. return ret;
  3705. }
  3706. int extent_readpages(struct extent_io_tree *tree,
  3707. struct address_space *mapping,
  3708. struct list_head *pages, unsigned nr_pages,
  3709. get_extent_t get_extent)
  3710. {
  3711. struct bio *bio = NULL;
  3712. unsigned page_idx;
  3713. unsigned long bio_flags = 0;
  3714. struct page *pagepool[16];
  3715. struct page *page;
  3716. struct extent_map *em_cached = NULL;
  3717. int nr = 0;
  3718. u64 prev_em_start = (u64)-1;
  3719. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3720. page = list_entry(pages->prev, struct page, lru);
  3721. prefetchw(&page->flags);
  3722. list_del(&page->lru);
  3723. if (add_to_page_cache_lru(page, mapping,
  3724. page->index,
  3725. readahead_gfp_mask(mapping))) {
  3726. put_page(page);
  3727. continue;
  3728. }
  3729. pagepool[nr++] = page;
  3730. if (nr < ARRAY_SIZE(pagepool))
  3731. continue;
  3732. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3733. &bio, 0, &bio_flags, READ, &prev_em_start);
  3734. nr = 0;
  3735. }
  3736. if (nr)
  3737. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3738. &bio, 0, &bio_flags, READ, &prev_em_start);
  3739. if (em_cached)
  3740. free_extent_map(em_cached);
  3741. BUG_ON(!list_empty(pages));
  3742. if (bio)
  3743. return submit_one_bio(READ, bio, 0, bio_flags);
  3744. return 0;
  3745. }
  3746. /*
  3747. * basic invalidatepage code, this waits on any locked or writeback
  3748. * ranges corresponding to the page, and then deletes any extent state
  3749. * records from the tree
  3750. */
  3751. int extent_invalidatepage(struct extent_io_tree *tree,
  3752. struct page *page, unsigned long offset)
  3753. {
  3754. struct extent_state *cached_state = NULL;
  3755. u64 start = page_offset(page);
  3756. u64 end = start + PAGE_SIZE - 1;
  3757. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3758. start += ALIGN(offset, blocksize);
  3759. if (start > end)
  3760. return 0;
  3761. lock_extent_bits(tree, start, end, &cached_state);
  3762. wait_on_page_writeback(page);
  3763. clear_extent_bit(tree, start, end,
  3764. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3765. EXTENT_DO_ACCOUNTING,
  3766. 1, 1, &cached_state, GFP_NOFS);
  3767. return 0;
  3768. }
  3769. /*
  3770. * a helper for releasepage, this tests for areas of the page that
  3771. * are locked or under IO and drops the related state bits if it is safe
  3772. * to drop the page.
  3773. */
  3774. static int try_release_extent_state(struct extent_map_tree *map,
  3775. struct extent_io_tree *tree,
  3776. struct page *page, gfp_t mask)
  3777. {
  3778. u64 start = page_offset(page);
  3779. u64 end = start + PAGE_SIZE - 1;
  3780. int ret = 1;
  3781. if (test_range_bit(tree, start, end,
  3782. EXTENT_IOBITS, 0, NULL))
  3783. ret = 0;
  3784. else {
  3785. if ((mask & GFP_NOFS) == GFP_NOFS)
  3786. mask = GFP_NOFS;
  3787. /*
  3788. * at this point we can safely clear everything except the
  3789. * locked bit and the nodatasum bit
  3790. */
  3791. ret = clear_extent_bit(tree, start, end,
  3792. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3793. 0, 0, NULL, mask);
  3794. /* if clear_extent_bit failed for enomem reasons,
  3795. * we can't allow the release to continue.
  3796. */
  3797. if (ret < 0)
  3798. ret = 0;
  3799. else
  3800. ret = 1;
  3801. }
  3802. return ret;
  3803. }
  3804. /*
  3805. * a helper for releasepage. As long as there are no locked extents
  3806. * in the range corresponding to the page, both state records and extent
  3807. * map records are removed
  3808. */
  3809. int try_release_extent_mapping(struct extent_map_tree *map,
  3810. struct extent_io_tree *tree, struct page *page,
  3811. gfp_t mask)
  3812. {
  3813. struct extent_map *em;
  3814. u64 start = page_offset(page);
  3815. u64 end = start + PAGE_SIZE - 1;
  3816. if (gfpflags_allow_blocking(mask) &&
  3817. page->mapping->host->i_size > SZ_16M) {
  3818. u64 len;
  3819. while (start <= end) {
  3820. len = end - start + 1;
  3821. write_lock(&map->lock);
  3822. em = lookup_extent_mapping(map, start, len);
  3823. if (!em) {
  3824. write_unlock(&map->lock);
  3825. break;
  3826. }
  3827. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3828. em->start != start) {
  3829. write_unlock(&map->lock);
  3830. free_extent_map(em);
  3831. break;
  3832. }
  3833. if (!test_range_bit(tree, em->start,
  3834. extent_map_end(em) - 1,
  3835. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3836. 0, NULL)) {
  3837. remove_extent_mapping(map, em);
  3838. /* once for the rb tree */
  3839. free_extent_map(em);
  3840. }
  3841. start = extent_map_end(em);
  3842. write_unlock(&map->lock);
  3843. /* once for us */
  3844. free_extent_map(em);
  3845. }
  3846. }
  3847. return try_release_extent_state(map, tree, page, mask);
  3848. }
  3849. /*
  3850. * helper function for fiemap, which doesn't want to see any holes.
  3851. * This maps until we find something past 'last'
  3852. */
  3853. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3854. u64 offset,
  3855. u64 last,
  3856. get_extent_t *get_extent)
  3857. {
  3858. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3859. struct extent_map *em;
  3860. u64 len;
  3861. if (offset >= last)
  3862. return NULL;
  3863. while (1) {
  3864. len = last - offset;
  3865. if (len == 0)
  3866. break;
  3867. len = ALIGN(len, sectorsize);
  3868. em = get_extent(inode, NULL, 0, offset, len, 0);
  3869. if (IS_ERR_OR_NULL(em))
  3870. return em;
  3871. /* if this isn't a hole return it */
  3872. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3873. em->block_start != EXTENT_MAP_HOLE) {
  3874. return em;
  3875. }
  3876. /* this is a hole, advance to the next extent */
  3877. offset = extent_map_end(em);
  3878. free_extent_map(em);
  3879. if (offset >= last)
  3880. break;
  3881. }
  3882. return NULL;
  3883. }
  3884. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3885. __u64 start, __u64 len, get_extent_t *get_extent)
  3886. {
  3887. int ret = 0;
  3888. u64 off = start;
  3889. u64 max = start + len;
  3890. u32 flags = 0;
  3891. u32 found_type;
  3892. u64 last;
  3893. u64 last_for_get_extent = 0;
  3894. u64 disko = 0;
  3895. u64 isize = i_size_read(inode);
  3896. struct btrfs_key found_key;
  3897. struct extent_map *em = NULL;
  3898. struct extent_state *cached_state = NULL;
  3899. struct btrfs_path *path;
  3900. struct btrfs_root *root = BTRFS_I(inode)->root;
  3901. int end = 0;
  3902. u64 em_start = 0;
  3903. u64 em_len = 0;
  3904. u64 em_end = 0;
  3905. if (len == 0)
  3906. return -EINVAL;
  3907. path = btrfs_alloc_path();
  3908. if (!path)
  3909. return -ENOMEM;
  3910. path->leave_spinning = 1;
  3911. start = round_down(start, BTRFS_I(inode)->root->sectorsize);
  3912. len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start;
  3913. /*
  3914. * lookup the last file extent. We're not using i_size here
  3915. * because there might be preallocation past i_size
  3916. */
  3917. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
  3918. 0);
  3919. if (ret < 0) {
  3920. btrfs_free_path(path);
  3921. return ret;
  3922. } else {
  3923. WARN_ON(!ret);
  3924. if (ret == 1)
  3925. ret = 0;
  3926. }
  3927. path->slots[0]--;
  3928. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3929. found_type = found_key.type;
  3930. /* No extents, but there might be delalloc bits */
  3931. if (found_key.objectid != btrfs_ino(inode) ||
  3932. found_type != BTRFS_EXTENT_DATA_KEY) {
  3933. /* have to trust i_size as the end */
  3934. last = (u64)-1;
  3935. last_for_get_extent = isize;
  3936. } else {
  3937. /*
  3938. * remember the start of the last extent. There are a
  3939. * bunch of different factors that go into the length of the
  3940. * extent, so its much less complex to remember where it started
  3941. */
  3942. last = found_key.offset;
  3943. last_for_get_extent = last + 1;
  3944. }
  3945. btrfs_release_path(path);
  3946. /*
  3947. * we might have some extents allocated but more delalloc past those
  3948. * extents. so, we trust isize unless the start of the last extent is
  3949. * beyond isize
  3950. */
  3951. if (last < isize) {
  3952. last = (u64)-1;
  3953. last_for_get_extent = isize;
  3954. }
  3955. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  3956. &cached_state);
  3957. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3958. get_extent);
  3959. if (!em)
  3960. goto out;
  3961. if (IS_ERR(em)) {
  3962. ret = PTR_ERR(em);
  3963. goto out;
  3964. }
  3965. while (!end) {
  3966. u64 offset_in_extent = 0;
  3967. /* break if the extent we found is outside the range */
  3968. if (em->start >= max || extent_map_end(em) < off)
  3969. break;
  3970. /*
  3971. * get_extent may return an extent that starts before our
  3972. * requested range. We have to make sure the ranges
  3973. * we return to fiemap always move forward and don't
  3974. * overlap, so adjust the offsets here
  3975. */
  3976. em_start = max(em->start, off);
  3977. /*
  3978. * record the offset from the start of the extent
  3979. * for adjusting the disk offset below. Only do this if the
  3980. * extent isn't compressed since our in ram offset may be past
  3981. * what we have actually allocated on disk.
  3982. */
  3983. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3984. offset_in_extent = em_start - em->start;
  3985. em_end = extent_map_end(em);
  3986. em_len = em_end - em_start;
  3987. disko = 0;
  3988. flags = 0;
  3989. /*
  3990. * bump off for our next call to get_extent
  3991. */
  3992. off = extent_map_end(em);
  3993. if (off >= max)
  3994. end = 1;
  3995. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3996. end = 1;
  3997. flags |= FIEMAP_EXTENT_LAST;
  3998. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3999. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  4000. FIEMAP_EXTENT_NOT_ALIGNED);
  4001. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  4002. flags |= (FIEMAP_EXTENT_DELALLOC |
  4003. FIEMAP_EXTENT_UNKNOWN);
  4004. } else if (fieinfo->fi_extents_max) {
  4005. u64 bytenr = em->block_start -
  4006. (em->start - em->orig_start);
  4007. disko = em->block_start + offset_in_extent;
  4008. /*
  4009. * As btrfs supports shared space, this information
  4010. * can be exported to userspace tools via
  4011. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  4012. * then we're just getting a count and we can skip the
  4013. * lookup stuff.
  4014. */
  4015. ret = btrfs_check_shared(NULL, root->fs_info,
  4016. root->objectid,
  4017. btrfs_ino(inode), bytenr);
  4018. if (ret < 0)
  4019. goto out_free;
  4020. if (ret)
  4021. flags |= FIEMAP_EXTENT_SHARED;
  4022. ret = 0;
  4023. }
  4024. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4025. flags |= FIEMAP_EXTENT_ENCODED;
  4026. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4027. flags |= FIEMAP_EXTENT_UNWRITTEN;
  4028. free_extent_map(em);
  4029. em = NULL;
  4030. if ((em_start >= last) || em_len == (u64)-1 ||
  4031. (last == (u64)-1 && isize <= em_end)) {
  4032. flags |= FIEMAP_EXTENT_LAST;
  4033. end = 1;
  4034. }
  4035. /* now scan forward to see if this is really the last extent. */
  4036. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4037. get_extent);
  4038. if (IS_ERR(em)) {
  4039. ret = PTR_ERR(em);
  4040. goto out;
  4041. }
  4042. if (!em) {
  4043. flags |= FIEMAP_EXTENT_LAST;
  4044. end = 1;
  4045. }
  4046. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  4047. em_len, flags);
  4048. if (ret) {
  4049. if (ret == 1)
  4050. ret = 0;
  4051. goto out_free;
  4052. }
  4053. }
  4054. out_free:
  4055. free_extent_map(em);
  4056. out:
  4057. btrfs_free_path(path);
  4058. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4059. &cached_state, GFP_NOFS);
  4060. return ret;
  4061. }
  4062. static void __free_extent_buffer(struct extent_buffer *eb)
  4063. {
  4064. btrfs_leak_debug_del(&eb->leak_list);
  4065. kmem_cache_free(extent_buffer_cache, eb);
  4066. }
  4067. int extent_buffer_under_io(struct extent_buffer *eb)
  4068. {
  4069. return (atomic_read(&eb->io_pages) ||
  4070. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4071. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4072. }
  4073. /*
  4074. * Helper for releasing extent buffer page.
  4075. */
  4076. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4077. {
  4078. unsigned long index;
  4079. struct page *page;
  4080. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4081. BUG_ON(extent_buffer_under_io(eb));
  4082. index = num_extent_pages(eb->start, eb->len);
  4083. if (index == 0)
  4084. return;
  4085. do {
  4086. index--;
  4087. page = eb->pages[index];
  4088. if (!page)
  4089. continue;
  4090. if (mapped)
  4091. spin_lock(&page->mapping->private_lock);
  4092. /*
  4093. * We do this since we'll remove the pages after we've
  4094. * removed the eb from the radix tree, so we could race
  4095. * and have this page now attached to the new eb. So
  4096. * only clear page_private if it's still connected to
  4097. * this eb.
  4098. */
  4099. if (PagePrivate(page) &&
  4100. page->private == (unsigned long)eb) {
  4101. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4102. BUG_ON(PageDirty(page));
  4103. BUG_ON(PageWriteback(page));
  4104. /*
  4105. * We need to make sure we haven't be attached
  4106. * to a new eb.
  4107. */
  4108. ClearPagePrivate(page);
  4109. set_page_private(page, 0);
  4110. /* One for the page private */
  4111. put_page(page);
  4112. }
  4113. if (mapped)
  4114. spin_unlock(&page->mapping->private_lock);
  4115. /* One for when we allocated the page */
  4116. put_page(page);
  4117. } while (index != 0);
  4118. }
  4119. /*
  4120. * Helper for releasing the extent buffer.
  4121. */
  4122. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4123. {
  4124. btrfs_release_extent_buffer_page(eb);
  4125. __free_extent_buffer(eb);
  4126. }
  4127. static struct extent_buffer *
  4128. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4129. unsigned long len)
  4130. {
  4131. struct extent_buffer *eb = NULL;
  4132. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
  4133. eb->start = start;
  4134. eb->len = len;
  4135. eb->fs_info = fs_info;
  4136. eb->bflags = 0;
  4137. rwlock_init(&eb->lock);
  4138. atomic_set(&eb->write_locks, 0);
  4139. atomic_set(&eb->read_locks, 0);
  4140. atomic_set(&eb->blocking_readers, 0);
  4141. atomic_set(&eb->blocking_writers, 0);
  4142. atomic_set(&eb->spinning_readers, 0);
  4143. atomic_set(&eb->spinning_writers, 0);
  4144. eb->lock_nested = 0;
  4145. init_waitqueue_head(&eb->write_lock_wq);
  4146. init_waitqueue_head(&eb->read_lock_wq);
  4147. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4148. spin_lock_init(&eb->refs_lock);
  4149. atomic_set(&eb->refs, 1);
  4150. atomic_set(&eb->io_pages, 0);
  4151. /*
  4152. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4153. */
  4154. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4155. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4156. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4157. return eb;
  4158. }
  4159. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4160. {
  4161. unsigned long i;
  4162. struct page *p;
  4163. struct extent_buffer *new;
  4164. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4165. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4166. if (new == NULL)
  4167. return NULL;
  4168. for (i = 0; i < num_pages; i++) {
  4169. p = alloc_page(GFP_NOFS);
  4170. if (!p) {
  4171. btrfs_release_extent_buffer(new);
  4172. return NULL;
  4173. }
  4174. attach_extent_buffer_page(new, p);
  4175. WARN_ON(PageDirty(p));
  4176. SetPageUptodate(p);
  4177. new->pages[i] = p;
  4178. }
  4179. copy_extent_buffer(new, src, 0, 0, src->len);
  4180. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4181. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4182. return new;
  4183. }
  4184. struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4185. u64 start, unsigned long len)
  4186. {
  4187. struct extent_buffer *eb;
  4188. unsigned long num_pages;
  4189. unsigned long i;
  4190. num_pages = num_extent_pages(start, len);
  4191. eb = __alloc_extent_buffer(fs_info, start, len);
  4192. if (!eb)
  4193. return NULL;
  4194. for (i = 0; i < num_pages; i++) {
  4195. eb->pages[i] = alloc_page(GFP_NOFS);
  4196. if (!eb->pages[i])
  4197. goto err;
  4198. }
  4199. set_extent_buffer_uptodate(eb);
  4200. btrfs_set_header_nritems(eb, 0);
  4201. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4202. return eb;
  4203. err:
  4204. for (; i > 0; i--)
  4205. __free_page(eb->pages[i - 1]);
  4206. __free_extent_buffer(eb);
  4207. return NULL;
  4208. }
  4209. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4210. u64 start, u32 nodesize)
  4211. {
  4212. unsigned long len;
  4213. if (!fs_info) {
  4214. /*
  4215. * Called only from tests that don't always have a fs_info
  4216. * available
  4217. */
  4218. len = nodesize;
  4219. } else {
  4220. len = fs_info->tree_root->nodesize;
  4221. }
  4222. return __alloc_dummy_extent_buffer(fs_info, start, len);
  4223. }
  4224. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4225. {
  4226. int refs;
  4227. /* the ref bit is tricky. We have to make sure it is set
  4228. * if we have the buffer dirty. Otherwise the
  4229. * code to free a buffer can end up dropping a dirty
  4230. * page
  4231. *
  4232. * Once the ref bit is set, it won't go away while the
  4233. * buffer is dirty or in writeback, and it also won't
  4234. * go away while we have the reference count on the
  4235. * eb bumped.
  4236. *
  4237. * We can't just set the ref bit without bumping the
  4238. * ref on the eb because free_extent_buffer might
  4239. * see the ref bit and try to clear it. If this happens
  4240. * free_extent_buffer might end up dropping our original
  4241. * ref by mistake and freeing the page before we are able
  4242. * to add one more ref.
  4243. *
  4244. * So bump the ref count first, then set the bit. If someone
  4245. * beat us to it, drop the ref we added.
  4246. */
  4247. refs = atomic_read(&eb->refs);
  4248. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4249. return;
  4250. spin_lock(&eb->refs_lock);
  4251. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4252. atomic_inc(&eb->refs);
  4253. spin_unlock(&eb->refs_lock);
  4254. }
  4255. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4256. struct page *accessed)
  4257. {
  4258. unsigned long num_pages, i;
  4259. check_buffer_tree_ref(eb);
  4260. num_pages = num_extent_pages(eb->start, eb->len);
  4261. for (i = 0; i < num_pages; i++) {
  4262. struct page *p = eb->pages[i];
  4263. if (p != accessed)
  4264. mark_page_accessed(p);
  4265. }
  4266. }
  4267. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4268. u64 start)
  4269. {
  4270. struct extent_buffer *eb;
  4271. rcu_read_lock();
  4272. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4273. start >> PAGE_SHIFT);
  4274. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4275. rcu_read_unlock();
  4276. /*
  4277. * Lock our eb's refs_lock to avoid races with
  4278. * free_extent_buffer. When we get our eb it might be flagged
  4279. * with EXTENT_BUFFER_STALE and another task running
  4280. * free_extent_buffer might have seen that flag set,
  4281. * eb->refs == 2, that the buffer isn't under IO (dirty and
  4282. * writeback flags not set) and it's still in the tree (flag
  4283. * EXTENT_BUFFER_TREE_REF set), therefore being in the process
  4284. * of decrementing the extent buffer's reference count twice.
  4285. * So here we could race and increment the eb's reference count,
  4286. * clear its stale flag, mark it as dirty and drop our reference
  4287. * before the other task finishes executing free_extent_buffer,
  4288. * which would later result in an attempt to free an extent
  4289. * buffer that is dirty.
  4290. */
  4291. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  4292. spin_lock(&eb->refs_lock);
  4293. spin_unlock(&eb->refs_lock);
  4294. }
  4295. mark_extent_buffer_accessed(eb, NULL);
  4296. return eb;
  4297. }
  4298. rcu_read_unlock();
  4299. return NULL;
  4300. }
  4301. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4302. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4303. u64 start, u32 nodesize)
  4304. {
  4305. struct extent_buffer *eb, *exists = NULL;
  4306. int ret;
  4307. eb = find_extent_buffer(fs_info, start);
  4308. if (eb)
  4309. return eb;
  4310. eb = alloc_dummy_extent_buffer(fs_info, start, nodesize);
  4311. if (!eb)
  4312. return NULL;
  4313. eb->fs_info = fs_info;
  4314. again:
  4315. ret = radix_tree_preload(GFP_NOFS);
  4316. if (ret)
  4317. goto free_eb;
  4318. spin_lock(&fs_info->buffer_lock);
  4319. ret = radix_tree_insert(&fs_info->buffer_radix,
  4320. start >> PAGE_SHIFT, eb);
  4321. spin_unlock(&fs_info->buffer_lock);
  4322. radix_tree_preload_end();
  4323. if (ret == -EEXIST) {
  4324. exists = find_extent_buffer(fs_info, start);
  4325. if (exists)
  4326. goto free_eb;
  4327. else
  4328. goto again;
  4329. }
  4330. check_buffer_tree_ref(eb);
  4331. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4332. /*
  4333. * We will free dummy extent buffer's if they come into
  4334. * free_extent_buffer with a ref count of 2, but if we are using this we
  4335. * want the buffers to stay in memory until we're done with them, so
  4336. * bump the ref count again.
  4337. */
  4338. atomic_inc(&eb->refs);
  4339. return eb;
  4340. free_eb:
  4341. btrfs_release_extent_buffer(eb);
  4342. return exists;
  4343. }
  4344. #endif
  4345. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4346. u64 start)
  4347. {
  4348. unsigned long len = fs_info->tree_root->nodesize;
  4349. unsigned long num_pages = num_extent_pages(start, len);
  4350. unsigned long i;
  4351. unsigned long index = start >> PAGE_SHIFT;
  4352. struct extent_buffer *eb;
  4353. struct extent_buffer *exists = NULL;
  4354. struct page *p;
  4355. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4356. int uptodate = 1;
  4357. int ret;
  4358. if (!IS_ALIGNED(start, fs_info->tree_root->sectorsize)) {
  4359. btrfs_err(fs_info, "bad tree block start %llu", start);
  4360. return ERR_PTR(-EINVAL);
  4361. }
  4362. eb = find_extent_buffer(fs_info, start);
  4363. if (eb)
  4364. return eb;
  4365. eb = __alloc_extent_buffer(fs_info, start, len);
  4366. if (!eb)
  4367. return ERR_PTR(-ENOMEM);
  4368. for (i = 0; i < num_pages; i++, index++) {
  4369. p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
  4370. if (!p) {
  4371. exists = ERR_PTR(-ENOMEM);
  4372. goto free_eb;
  4373. }
  4374. spin_lock(&mapping->private_lock);
  4375. if (PagePrivate(p)) {
  4376. /*
  4377. * We could have already allocated an eb for this page
  4378. * and attached one so lets see if we can get a ref on
  4379. * the existing eb, and if we can we know it's good and
  4380. * we can just return that one, else we know we can just
  4381. * overwrite page->private.
  4382. */
  4383. exists = (struct extent_buffer *)p->private;
  4384. if (atomic_inc_not_zero(&exists->refs)) {
  4385. spin_unlock(&mapping->private_lock);
  4386. unlock_page(p);
  4387. put_page(p);
  4388. mark_extent_buffer_accessed(exists, p);
  4389. goto free_eb;
  4390. }
  4391. exists = NULL;
  4392. /*
  4393. * Do this so attach doesn't complain and we need to
  4394. * drop the ref the old guy had.
  4395. */
  4396. ClearPagePrivate(p);
  4397. WARN_ON(PageDirty(p));
  4398. put_page(p);
  4399. }
  4400. attach_extent_buffer_page(eb, p);
  4401. spin_unlock(&mapping->private_lock);
  4402. WARN_ON(PageDirty(p));
  4403. eb->pages[i] = p;
  4404. if (!PageUptodate(p))
  4405. uptodate = 0;
  4406. /*
  4407. * see below about how we avoid a nasty race with release page
  4408. * and why we unlock later
  4409. */
  4410. }
  4411. if (uptodate)
  4412. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4413. again:
  4414. ret = radix_tree_preload(GFP_NOFS);
  4415. if (ret) {
  4416. exists = ERR_PTR(ret);
  4417. goto free_eb;
  4418. }
  4419. spin_lock(&fs_info->buffer_lock);
  4420. ret = radix_tree_insert(&fs_info->buffer_radix,
  4421. start >> PAGE_SHIFT, eb);
  4422. spin_unlock(&fs_info->buffer_lock);
  4423. radix_tree_preload_end();
  4424. if (ret == -EEXIST) {
  4425. exists = find_extent_buffer(fs_info, start);
  4426. if (exists)
  4427. goto free_eb;
  4428. else
  4429. goto again;
  4430. }
  4431. /* add one reference for the tree */
  4432. check_buffer_tree_ref(eb);
  4433. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4434. /*
  4435. * there is a race where release page may have
  4436. * tried to find this extent buffer in the radix
  4437. * but failed. It will tell the VM it is safe to
  4438. * reclaim the, and it will clear the page private bit.
  4439. * We must make sure to set the page private bit properly
  4440. * after the extent buffer is in the radix tree so
  4441. * it doesn't get lost
  4442. */
  4443. SetPageChecked(eb->pages[0]);
  4444. for (i = 1; i < num_pages; i++) {
  4445. p = eb->pages[i];
  4446. ClearPageChecked(p);
  4447. unlock_page(p);
  4448. }
  4449. unlock_page(eb->pages[0]);
  4450. return eb;
  4451. free_eb:
  4452. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4453. for (i = 0; i < num_pages; i++) {
  4454. if (eb->pages[i])
  4455. unlock_page(eb->pages[i]);
  4456. }
  4457. btrfs_release_extent_buffer(eb);
  4458. return exists;
  4459. }
  4460. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4461. {
  4462. struct extent_buffer *eb =
  4463. container_of(head, struct extent_buffer, rcu_head);
  4464. __free_extent_buffer(eb);
  4465. }
  4466. /* Expects to have eb->eb_lock already held */
  4467. static int release_extent_buffer(struct extent_buffer *eb)
  4468. {
  4469. WARN_ON(atomic_read(&eb->refs) == 0);
  4470. if (atomic_dec_and_test(&eb->refs)) {
  4471. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4472. struct btrfs_fs_info *fs_info = eb->fs_info;
  4473. spin_unlock(&eb->refs_lock);
  4474. spin_lock(&fs_info->buffer_lock);
  4475. radix_tree_delete(&fs_info->buffer_radix,
  4476. eb->start >> PAGE_SHIFT);
  4477. spin_unlock(&fs_info->buffer_lock);
  4478. } else {
  4479. spin_unlock(&eb->refs_lock);
  4480. }
  4481. /* Should be safe to release our pages at this point */
  4482. btrfs_release_extent_buffer_page(eb);
  4483. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4484. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4485. __free_extent_buffer(eb);
  4486. return 1;
  4487. }
  4488. #endif
  4489. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4490. return 1;
  4491. }
  4492. spin_unlock(&eb->refs_lock);
  4493. return 0;
  4494. }
  4495. void free_extent_buffer(struct extent_buffer *eb)
  4496. {
  4497. int refs;
  4498. int old;
  4499. if (!eb)
  4500. return;
  4501. while (1) {
  4502. refs = atomic_read(&eb->refs);
  4503. if (refs <= 3)
  4504. break;
  4505. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4506. if (old == refs)
  4507. return;
  4508. }
  4509. spin_lock(&eb->refs_lock);
  4510. if (atomic_read(&eb->refs) == 2 &&
  4511. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4512. atomic_dec(&eb->refs);
  4513. if (atomic_read(&eb->refs) == 2 &&
  4514. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4515. !extent_buffer_under_io(eb) &&
  4516. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4517. atomic_dec(&eb->refs);
  4518. /*
  4519. * I know this is terrible, but it's temporary until we stop tracking
  4520. * the uptodate bits and such for the extent buffers.
  4521. */
  4522. release_extent_buffer(eb);
  4523. }
  4524. void free_extent_buffer_stale(struct extent_buffer *eb)
  4525. {
  4526. if (!eb)
  4527. return;
  4528. spin_lock(&eb->refs_lock);
  4529. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4530. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4531. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4532. atomic_dec(&eb->refs);
  4533. release_extent_buffer(eb);
  4534. }
  4535. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4536. {
  4537. unsigned long i;
  4538. unsigned long num_pages;
  4539. struct page *page;
  4540. num_pages = num_extent_pages(eb->start, eb->len);
  4541. for (i = 0; i < num_pages; i++) {
  4542. page = eb->pages[i];
  4543. if (!PageDirty(page))
  4544. continue;
  4545. lock_page(page);
  4546. WARN_ON(!PagePrivate(page));
  4547. clear_page_dirty_for_io(page);
  4548. spin_lock_irq(&page->mapping->tree_lock);
  4549. if (!PageDirty(page)) {
  4550. radix_tree_tag_clear(&page->mapping->page_tree,
  4551. page_index(page),
  4552. PAGECACHE_TAG_DIRTY);
  4553. }
  4554. spin_unlock_irq(&page->mapping->tree_lock);
  4555. ClearPageError(page);
  4556. unlock_page(page);
  4557. }
  4558. WARN_ON(atomic_read(&eb->refs) == 0);
  4559. }
  4560. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4561. {
  4562. unsigned long i;
  4563. unsigned long num_pages;
  4564. int was_dirty = 0;
  4565. check_buffer_tree_ref(eb);
  4566. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4567. num_pages = num_extent_pages(eb->start, eb->len);
  4568. WARN_ON(atomic_read(&eb->refs) == 0);
  4569. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4570. for (i = 0; i < num_pages; i++)
  4571. set_page_dirty(eb->pages[i]);
  4572. return was_dirty;
  4573. }
  4574. void clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4575. {
  4576. unsigned long i;
  4577. struct page *page;
  4578. unsigned long num_pages;
  4579. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4580. num_pages = num_extent_pages(eb->start, eb->len);
  4581. for (i = 0; i < num_pages; i++) {
  4582. page = eb->pages[i];
  4583. if (page)
  4584. ClearPageUptodate(page);
  4585. }
  4586. }
  4587. void set_extent_buffer_uptodate(struct extent_buffer *eb)
  4588. {
  4589. unsigned long i;
  4590. struct page *page;
  4591. unsigned long num_pages;
  4592. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4593. num_pages = num_extent_pages(eb->start, eb->len);
  4594. for (i = 0; i < num_pages; i++) {
  4595. page = eb->pages[i];
  4596. SetPageUptodate(page);
  4597. }
  4598. }
  4599. int extent_buffer_uptodate(struct extent_buffer *eb)
  4600. {
  4601. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4602. }
  4603. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4604. struct extent_buffer *eb, u64 start, int wait,
  4605. get_extent_t *get_extent, int mirror_num)
  4606. {
  4607. unsigned long i;
  4608. unsigned long start_i;
  4609. struct page *page;
  4610. int err;
  4611. int ret = 0;
  4612. int locked_pages = 0;
  4613. int all_uptodate = 1;
  4614. unsigned long num_pages;
  4615. unsigned long num_reads = 0;
  4616. struct bio *bio = NULL;
  4617. unsigned long bio_flags = 0;
  4618. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4619. return 0;
  4620. if (start) {
  4621. WARN_ON(start < eb->start);
  4622. start_i = (start >> PAGE_SHIFT) -
  4623. (eb->start >> PAGE_SHIFT);
  4624. } else {
  4625. start_i = 0;
  4626. }
  4627. num_pages = num_extent_pages(eb->start, eb->len);
  4628. for (i = start_i; i < num_pages; i++) {
  4629. page = eb->pages[i];
  4630. if (wait == WAIT_NONE) {
  4631. if (!trylock_page(page))
  4632. goto unlock_exit;
  4633. } else {
  4634. lock_page(page);
  4635. }
  4636. locked_pages++;
  4637. if (!PageUptodate(page)) {
  4638. num_reads++;
  4639. all_uptodate = 0;
  4640. }
  4641. }
  4642. if (all_uptodate) {
  4643. if (start_i == 0)
  4644. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4645. goto unlock_exit;
  4646. }
  4647. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4648. eb->read_mirror = 0;
  4649. atomic_set(&eb->io_pages, num_reads);
  4650. for (i = start_i; i < num_pages; i++) {
  4651. page = eb->pages[i];
  4652. if (!PageUptodate(page)) {
  4653. ClearPageError(page);
  4654. err = __extent_read_full_page(tree, page,
  4655. get_extent, &bio,
  4656. mirror_num, &bio_flags,
  4657. READ | REQ_META);
  4658. if (err)
  4659. ret = err;
  4660. } else {
  4661. unlock_page(page);
  4662. }
  4663. }
  4664. if (bio) {
  4665. err = submit_one_bio(READ | REQ_META, bio, mirror_num,
  4666. bio_flags);
  4667. if (err)
  4668. return err;
  4669. }
  4670. if (ret || wait != WAIT_COMPLETE)
  4671. return ret;
  4672. for (i = start_i; i < num_pages; i++) {
  4673. page = eb->pages[i];
  4674. wait_on_page_locked(page);
  4675. if (!PageUptodate(page))
  4676. ret = -EIO;
  4677. }
  4678. return ret;
  4679. unlock_exit:
  4680. i = start_i;
  4681. while (locked_pages > 0) {
  4682. page = eb->pages[i];
  4683. i++;
  4684. unlock_page(page);
  4685. locked_pages--;
  4686. }
  4687. return ret;
  4688. }
  4689. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4690. unsigned long start,
  4691. unsigned long len)
  4692. {
  4693. size_t cur;
  4694. size_t offset;
  4695. struct page *page;
  4696. char *kaddr;
  4697. char *dst = (char *)dstv;
  4698. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4699. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4700. WARN_ON(start > eb->len);
  4701. WARN_ON(start + len > eb->start + eb->len);
  4702. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4703. while (len > 0) {
  4704. page = eb->pages[i];
  4705. cur = min(len, (PAGE_SIZE - offset));
  4706. kaddr = page_address(page);
  4707. memcpy(dst, kaddr + offset, cur);
  4708. dst += cur;
  4709. len -= cur;
  4710. offset = 0;
  4711. i++;
  4712. }
  4713. }
  4714. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
  4715. unsigned long start,
  4716. unsigned long len)
  4717. {
  4718. size_t cur;
  4719. size_t offset;
  4720. struct page *page;
  4721. char *kaddr;
  4722. char __user *dst = (char __user *)dstv;
  4723. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4724. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4725. int ret = 0;
  4726. WARN_ON(start > eb->len);
  4727. WARN_ON(start + len > eb->start + eb->len);
  4728. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4729. while (len > 0) {
  4730. page = eb->pages[i];
  4731. cur = min(len, (PAGE_SIZE - offset));
  4732. kaddr = page_address(page);
  4733. if (copy_to_user(dst, kaddr + offset, cur)) {
  4734. ret = -EFAULT;
  4735. break;
  4736. }
  4737. dst += cur;
  4738. len -= cur;
  4739. offset = 0;
  4740. i++;
  4741. }
  4742. return ret;
  4743. }
  4744. /*
  4745. * return 0 if the item is found within a page.
  4746. * return 1 if the item spans two pages.
  4747. * return -EINVAL otherwise.
  4748. */
  4749. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4750. unsigned long min_len, char **map,
  4751. unsigned long *map_start,
  4752. unsigned long *map_len)
  4753. {
  4754. size_t offset = start & (PAGE_SIZE - 1);
  4755. char *kaddr;
  4756. struct page *p;
  4757. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4758. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4759. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4760. PAGE_SHIFT;
  4761. if (i != end_i)
  4762. return 1;
  4763. if (i == 0) {
  4764. offset = start_offset;
  4765. *map_start = 0;
  4766. } else {
  4767. offset = 0;
  4768. *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
  4769. }
  4770. if (start + min_len > eb->len) {
  4771. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  4772. "wanted %lu %lu\n",
  4773. eb->start, eb->len, start, min_len);
  4774. return -EINVAL;
  4775. }
  4776. p = eb->pages[i];
  4777. kaddr = page_address(p);
  4778. *map = kaddr + offset;
  4779. *map_len = PAGE_SIZE - offset;
  4780. return 0;
  4781. }
  4782. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4783. unsigned long start,
  4784. unsigned long len)
  4785. {
  4786. size_t cur;
  4787. size_t offset;
  4788. struct page *page;
  4789. char *kaddr;
  4790. char *ptr = (char *)ptrv;
  4791. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4792. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4793. int ret = 0;
  4794. WARN_ON(start > eb->len);
  4795. WARN_ON(start + len > eb->start + eb->len);
  4796. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4797. while (len > 0) {
  4798. page = eb->pages[i];
  4799. cur = min(len, (PAGE_SIZE - offset));
  4800. kaddr = page_address(page);
  4801. ret = memcmp(ptr, kaddr + offset, cur);
  4802. if (ret)
  4803. break;
  4804. ptr += cur;
  4805. len -= cur;
  4806. offset = 0;
  4807. i++;
  4808. }
  4809. return ret;
  4810. }
  4811. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4812. unsigned long start, unsigned long len)
  4813. {
  4814. size_t cur;
  4815. size_t offset;
  4816. struct page *page;
  4817. char *kaddr;
  4818. char *src = (char *)srcv;
  4819. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4820. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4821. WARN_ON(start > eb->len);
  4822. WARN_ON(start + len > eb->start + eb->len);
  4823. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4824. while (len > 0) {
  4825. page = eb->pages[i];
  4826. WARN_ON(!PageUptodate(page));
  4827. cur = min(len, PAGE_SIZE - offset);
  4828. kaddr = page_address(page);
  4829. memcpy(kaddr + offset, src, cur);
  4830. src += cur;
  4831. len -= cur;
  4832. offset = 0;
  4833. i++;
  4834. }
  4835. }
  4836. void memset_extent_buffer(struct extent_buffer *eb, char c,
  4837. unsigned long start, unsigned long len)
  4838. {
  4839. size_t cur;
  4840. size_t offset;
  4841. struct page *page;
  4842. char *kaddr;
  4843. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4844. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4845. WARN_ON(start > eb->len);
  4846. WARN_ON(start + len > eb->start + eb->len);
  4847. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4848. while (len > 0) {
  4849. page = eb->pages[i];
  4850. WARN_ON(!PageUptodate(page));
  4851. cur = min(len, PAGE_SIZE - offset);
  4852. kaddr = page_address(page);
  4853. memset(kaddr + offset, c, cur);
  4854. len -= cur;
  4855. offset = 0;
  4856. i++;
  4857. }
  4858. }
  4859. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4860. unsigned long dst_offset, unsigned long src_offset,
  4861. unsigned long len)
  4862. {
  4863. u64 dst_len = dst->len;
  4864. size_t cur;
  4865. size_t offset;
  4866. struct page *page;
  4867. char *kaddr;
  4868. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  4869. unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
  4870. WARN_ON(src->len != dst_len);
  4871. offset = (start_offset + dst_offset) &
  4872. (PAGE_SIZE - 1);
  4873. while (len > 0) {
  4874. page = dst->pages[i];
  4875. WARN_ON(!PageUptodate(page));
  4876. cur = min(len, (unsigned long)(PAGE_SIZE - offset));
  4877. kaddr = page_address(page);
  4878. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4879. src_offset += cur;
  4880. len -= cur;
  4881. offset = 0;
  4882. i++;
  4883. }
  4884. }
  4885. /*
  4886. * The extent buffer bitmap operations are done with byte granularity because
  4887. * bitmap items are not guaranteed to be aligned to a word and therefore a
  4888. * single word in a bitmap may straddle two pages in the extent buffer.
  4889. */
  4890. #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
  4891. #define BYTE_MASK ((1 << BITS_PER_BYTE) - 1)
  4892. #define BITMAP_FIRST_BYTE_MASK(start) \
  4893. ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
  4894. #define BITMAP_LAST_BYTE_MASK(nbits) \
  4895. (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
  4896. /*
  4897. * eb_bitmap_offset() - calculate the page and offset of the byte containing the
  4898. * given bit number
  4899. * @eb: the extent buffer
  4900. * @start: offset of the bitmap item in the extent buffer
  4901. * @nr: bit number
  4902. * @page_index: return index of the page in the extent buffer that contains the
  4903. * given bit number
  4904. * @page_offset: return offset into the page given by page_index
  4905. *
  4906. * This helper hides the ugliness of finding the byte in an extent buffer which
  4907. * contains a given bit.
  4908. */
  4909. static inline void eb_bitmap_offset(struct extent_buffer *eb,
  4910. unsigned long start, unsigned long nr,
  4911. unsigned long *page_index,
  4912. size_t *page_offset)
  4913. {
  4914. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4915. size_t byte_offset = BIT_BYTE(nr);
  4916. size_t offset;
  4917. /*
  4918. * The byte we want is the offset of the extent buffer + the offset of
  4919. * the bitmap item in the extent buffer + the offset of the byte in the
  4920. * bitmap item.
  4921. */
  4922. offset = start_offset + start + byte_offset;
  4923. *page_index = offset >> PAGE_SHIFT;
  4924. *page_offset = offset & (PAGE_SIZE - 1);
  4925. }
  4926. /**
  4927. * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
  4928. * @eb: the extent buffer
  4929. * @start: offset of the bitmap item in the extent buffer
  4930. * @nr: bit number to test
  4931. */
  4932. int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
  4933. unsigned long nr)
  4934. {
  4935. char *kaddr;
  4936. struct page *page;
  4937. unsigned long i;
  4938. size_t offset;
  4939. eb_bitmap_offset(eb, start, nr, &i, &offset);
  4940. page = eb->pages[i];
  4941. WARN_ON(!PageUptodate(page));
  4942. kaddr = page_address(page);
  4943. return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
  4944. }
  4945. /**
  4946. * extent_buffer_bitmap_set - set an area of a bitmap
  4947. * @eb: the extent buffer
  4948. * @start: offset of the bitmap item in the extent buffer
  4949. * @pos: bit number of the first bit
  4950. * @len: number of bits to set
  4951. */
  4952. void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
  4953. unsigned long pos, unsigned long len)
  4954. {
  4955. char *kaddr;
  4956. struct page *page;
  4957. unsigned long i;
  4958. size_t offset;
  4959. const unsigned int size = pos + len;
  4960. int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  4961. unsigned int mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
  4962. eb_bitmap_offset(eb, start, pos, &i, &offset);
  4963. page = eb->pages[i];
  4964. WARN_ON(!PageUptodate(page));
  4965. kaddr = page_address(page);
  4966. while (len >= bits_to_set) {
  4967. kaddr[offset] |= mask_to_set;
  4968. len -= bits_to_set;
  4969. bits_to_set = BITS_PER_BYTE;
  4970. mask_to_set = ~0U;
  4971. if (++offset >= PAGE_SIZE && len > 0) {
  4972. offset = 0;
  4973. page = eb->pages[++i];
  4974. WARN_ON(!PageUptodate(page));
  4975. kaddr = page_address(page);
  4976. }
  4977. }
  4978. if (len) {
  4979. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  4980. kaddr[offset] |= mask_to_set;
  4981. }
  4982. }
  4983. /**
  4984. * extent_buffer_bitmap_clear - clear an area of a bitmap
  4985. * @eb: the extent buffer
  4986. * @start: offset of the bitmap item in the extent buffer
  4987. * @pos: bit number of the first bit
  4988. * @len: number of bits to clear
  4989. */
  4990. void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
  4991. unsigned long pos, unsigned long len)
  4992. {
  4993. char *kaddr;
  4994. struct page *page;
  4995. unsigned long i;
  4996. size_t offset;
  4997. const unsigned int size = pos + len;
  4998. int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  4999. unsigned int mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
  5000. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5001. page = eb->pages[i];
  5002. WARN_ON(!PageUptodate(page));
  5003. kaddr = page_address(page);
  5004. while (len >= bits_to_clear) {
  5005. kaddr[offset] &= ~mask_to_clear;
  5006. len -= bits_to_clear;
  5007. bits_to_clear = BITS_PER_BYTE;
  5008. mask_to_clear = ~0U;
  5009. if (++offset >= PAGE_SIZE && len > 0) {
  5010. offset = 0;
  5011. page = eb->pages[++i];
  5012. WARN_ON(!PageUptodate(page));
  5013. kaddr = page_address(page);
  5014. }
  5015. }
  5016. if (len) {
  5017. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5018. kaddr[offset] &= ~mask_to_clear;
  5019. }
  5020. }
  5021. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  5022. {
  5023. unsigned long distance = (src > dst) ? src - dst : dst - src;
  5024. return distance < len;
  5025. }
  5026. static void copy_pages(struct page *dst_page, struct page *src_page,
  5027. unsigned long dst_off, unsigned long src_off,
  5028. unsigned long len)
  5029. {
  5030. char *dst_kaddr = page_address(dst_page);
  5031. char *src_kaddr;
  5032. int must_memmove = 0;
  5033. if (dst_page != src_page) {
  5034. src_kaddr = page_address(src_page);
  5035. } else {
  5036. src_kaddr = dst_kaddr;
  5037. if (areas_overlap(src_off, dst_off, len))
  5038. must_memmove = 1;
  5039. }
  5040. if (must_memmove)
  5041. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5042. else
  5043. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5044. }
  5045. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5046. unsigned long src_offset, unsigned long len)
  5047. {
  5048. size_t cur;
  5049. size_t dst_off_in_page;
  5050. size_t src_off_in_page;
  5051. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5052. unsigned long dst_i;
  5053. unsigned long src_i;
  5054. if (src_offset + len > dst->len) {
  5055. btrfs_err(dst->fs_info,
  5056. "memmove bogus src_offset %lu move "
  5057. "len %lu dst len %lu", src_offset, len, dst->len);
  5058. BUG_ON(1);
  5059. }
  5060. if (dst_offset + len > dst->len) {
  5061. btrfs_err(dst->fs_info,
  5062. "memmove bogus dst_offset %lu move "
  5063. "len %lu dst len %lu", dst_offset, len, dst->len);
  5064. BUG_ON(1);
  5065. }
  5066. while (len > 0) {
  5067. dst_off_in_page = (start_offset + dst_offset) &
  5068. (PAGE_SIZE - 1);
  5069. src_off_in_page = (start_offset + src_offset) &
  5070. (PAGE_SIZE - 1);
  5071. dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5072. src_i = (start_offset + src_offset) >> PAGE_SHIFT;
  5073. cur = min(len, (unsigned long)(PAGE_SIZE -
  5074. src_off_in_page));
  5075. cur = min_t(unsigned long, cur,
  5076. (unsigned long)(PAGE_SIZE - dst_off_in_page));
  5077. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5078. dst_off_in_page, src_off_in_page, cur);
  5079. src_offset += cur;
  5080. dst_offset += cur;
  5081. len -= cur;
  5082. }
  5083. }
  5084. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5085. unsigned long src_offset, unsigned long len)
  5086. {
  5087. size_t cur;
  5088. size_t dst_off_in_page;
  5089. size_t src_off_in_page;
  5090. unsigned long dst_end = dst_offset + len - 1;
  5091. unsigned long src_end = src_offset + len - 1;
  5092. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5093. unsigned long dst_i;
  5094. unsigned long src_i;
  5095. if (src_offset + len > dst->len) {
  5096. btrfs_err(dst->fs_info, "memmove bogus src_offset %lu move "
  5097. "len %lu len %lu", src_offset, len, dst->len);
  5098. BUG_ON(1);
  5099. }
  5100. if (dst_offset + len > dst->len) {
  5101. btrfs_err(dst->fs_info, "memmove bogus dst_offset %lu move "
  5102. "len %lu len %lu", dst_offset, len, dst->len);
  5103. BUG_ON(1);
  5104. }
  5105. if (dst_offset < src_offset) {
  5106. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  5107. return;
  5108. }
  5109. while (len > 0) {
  5110. dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
  5111. src_i = (start_offset + src_end) >> PAGE_SHIFT;
  5112. dst_off_in_page = (start_offset + dst_end) &
  5113. (PAGE_SIZE - 1);
  5114. src_off_in_page = (start_offset + src_end) &
  5115. (PAGE_SIZE - 1);
  5116. cur = min_t(unsigned long, len, src_off_in_page + 1);
  5117. cur = min(cur, dst_off_in_page + 1);
  5118. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5119. dst_off_in_page - cur + 1,
  5120. src_off_in_page - cur + 1, cur);
  5121. dst_end -= cur;
  5122. src_end -= cur;
  5123. len -= cur;
  5124. }
  5125. }
  5126. int try_release_extent_buffer(struct page *page)
  5127. {
  5128. struct extent_buffer *eb;
  5129. /*
  5130. * We need to make sure nobody is attaching this page to an eb right
  5131. * now.
  5132. */
  5133. spin_lock(&page->mapping->private_lock);
  5134. if (!PagePrivate(page)) {
  5135. spin_unlock(&page->mapping->private_lock);
  5136. return 1;
  5137. }
  5138. eb = (struct extent_buffer *)page->private;
  5139. BUG_ON(!eb);
  5140. /*
  5141. * This is a little awful but should be ok, we need to make sure that
  5142. * the eb doesn't disappear out from under us while we're looking at
  5143. * this page.
  5144. */
  5145. spin_lock(&eb->refs_lock);
  5146. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  5147. spin_unlock(&eb->refs_lock);
  5148. spin_unlock(&page->mapping->private_lock);
  5149. return 0;
  5150. }
  5151. spin_unlock(&page->mapping->private_lock);
  5152. /*
  5153. * If tree ref isn't set then we know the ref on this eb is a real ref,
  5154. * so just return, this page will likely be freed soon anyway.
  5155. */
  5156. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  5157. spin_unlock(&eb->refs_lock);
  5158. return 0;
  5159. }
  5160. return release_extent_buffer(eb);
  5161. }