extent_io.c 93 KB

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