extent_io.c 94 KB

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