extent_io.c 123 KB

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