extent_io.c 126 KB

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