extent_io.c 124 KB

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