extent_io.c 124 KB

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