extent_io.c 120 KB

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