extent_io.c 109 KB

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