extent_io.c 123 KB

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