extent_io.c 99 KB

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