extent_io.c 95 KB

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