extent_io.c 131 KB

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