extent_io.c 131 KB

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