extent_io.c 93 KB

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