extent_io.c 94 KB

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