extent_io.c 109 KB

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