extent_io.c 109 KB

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