extent_io.c 110 KB

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