extent_io.c 131 KB

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