extent_io.c 125 KB

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