extent_io.c 94 KB

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