extent_io.c 92 KB

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