extent_io.c 131 KB

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