extent_io.c 139 KB

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