extent_io.c 123 KB

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