extent_io.c 124 KB

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