extent_io.c 131 KB

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