extent_io.c 124 KB

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