extent_io.c 110 KB

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