extent_io.c 128 KB

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