extent_io.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837
  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. bool fill_delalloc = true;
  1924. if (wbc->sync_mode == WB_SYNC_ALL)
  1925. write_flags = WRITE_SYNC;
  1926. else
  1927. write_flags = WRITE;
  1928. trace___extent_writepage(page, inode, wbc);
  1929. WARN_ON(!PageLocked(page));
  1930. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  1931. if (page->index > end_index ||
  1932. (page->index == end_index && !pg_offset)) {
  1933. page->mapping->a_ops->invalidatepage(page, 0);
  1934. unlock_page(page);
  1935. return 0;
  1936. }
  1937. if (page->index == end_index) {
  1938. char *userpage;
  1939. userpage = kmap_atomic(page, KM_USER0);
  1940. memset(userpage + pg_offset, 0,
  1941. PAGE_CACHE_SIZE - pg_offset);
  1942. kunmap_atomic(userpage, KM_USER0);
  1943. flush_dcache_page(page);
  1944. }
  1945. pg_offset = 0;
  1946. set_page_extent_mapped(page);
  1947. if (!tree->ops || !tree->ops->fill_delalloc)
  1948. fill_delalloc = false;
  1949. delalloc_start = start;
  1950. delalloc_end = 0;
  1951. page_started = 0;
  1952. if (!epd->extent_locked && fill_delalloc) {
  1953. u64 delalloc_to_write = 0;
  1954. /*
  1955. * make sure the wbc mapping index is at least updated
  1956. * to this page.
  1957. */
  1958. update_nr_written(page, wbc, 0);
  1959. while (delalloc_end < page_end) {
  1960. nr_delalloc = find_lock_delalloc_range(inode, tree,
  1961. page,
  1962. &delalloc_start,
  1963. &delalloc_end,
  1964. 128 * 1024 * 1024);
  1965. if (nr_delalloc == 0) {
  1966. delalloc_start = delalloc_end + 1;
  1967. continue;
  1968. }
  1969. tree->ops->fill_delalloc(inode, page, delalloc_start,
  1970. delalloc_end, &page_started,
  1971. &nr_written);
  1972. /*
  1973. * delalloc_end is already one less than the total
  1974. * length, so we don't subtract one from
  1975. * PAGE_CACHE_SIZE
  1976. */
  1977. delalloc_to_write += (delalloc_end - delalloc_start +
  1978. PAGE_CACHE_SIZE) >>
  1979. PAGE_CACHE_SHIFT;
  1980. delalloc_start = delalloc_end + 1;
  1981. }
  1982. if (wbc->nr_to_write < delalloc_to_write) {
  1983. int thresh = 8192;
  1984. if (delalloc_to_write < thresh * 2)
  1985. thresh = delalloc_to_write;
  1986. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  1987. thresh);
  1988. }
  1989. /* did the fill delalloc function already unlock and start
  1990. * the IO?
  1991. */
  1992. if (page_started) {
  1993. ret = 0;
  1994. /*
  1995. * we've unlocked the page, so we can't update
  1996. * the mapping's writeback index, just update
  1997. * nr_to_write.
  1998. */
  1999. wbc->nr_to_write -= nr_written;
  2000. goto done_unlocked;
  2001. }
  2002. }
  2003. if (tree->ops && tree->ops->writepage_start_hook) {
  2004. ret = tree->ops->writepage_start_hook(page, start,
  2005. page_end);
  2006. if (ret == -EAGAIN) {
  2007. redirty_page_for_writepage(wbc, page);
  2008. update_nr_written(page, wbc, nr_written);
  2009. unlock_page(page);
  2010. ret = 0;
  2011. goto done_unlocked;
  2012. }
  2013. }
  2014. /*
  2015. * we don't want to touch the inode after unlocking the page,
  2016. * so we update the mapping writeback index now
  2017. */
  2018. update_nr_written(page, wbc, nr_written + 1);
  2019. end = page_end;
  2020. if (last_byte <= start) {
  2021. if (tree->ops && tree->ops->writepage_end_io_hook)
  2022. tree->ops->writepage_end_io_hook(page, start,
  2023. page_end, NULL, 1);
  2024. goto done;
  2025. }
  2026. blocksize = inode->i_sb->s_blocksize;
  2027. while (cur <= end) {
  2028. if (cur >= last_byte) {
  2029. if (tree->ops && tree->ops->writepage_end_io_hook)
  2030. tree->ops->writepage_end_io_hook(page, cur,
  2031. page_end, NULL, 1);
  2032. break;
  2033. }
  2034. em = epd->get_extent(inode, page, pg_offset, cur,
  2035. end - cur + 1, 1);
  2036. if (IS_ERR_OR_NULL(em)) {
  2037. SetPageError(page);
  2038. break;
  2039. }
  2040. extent_offset = cur - em->start;
  2041. BUG_ON(extent_map_end(em) <= cur);
  2042. BUG_ON(end < cur);
  2043. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2044. iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
  2045. sector = (em->block_start + extent_offset) >> 9;
  2046. bdev = em->bdev;
  2047. block_start = em->block_start;
  2048. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  2049. free_extent_map(em);
  2050. em = NULL;
  2051. /*
  2052. * compressed and inline extents are written through other
  2053. * paths in the FS
  2054. */
  2055. if (compressed || block_start == EXTENT_MAP_HOLE ||
  2056. block_start == EXTENT_MAP_INLINE) {
  2057. /*
  2058. * end_io notification does not happen here for
  2059. * compressed extents
  2060. */
  2061. if (!compressed && tree->ops &&
  2062. tree->ops->writepage_end_io_hook)
  2063. tree->ops->writepage_end_io_hook(page, cur,
  2064. cur + iosize - 1,
  2065. NULL, 1);
  2066. else if (compressed) {
  2067. /* we don't want to end_page_writeback on
  2068. * a compressed extent. this happens
  2069. * elsewhere
  2070. */
  2071. nr++;
  2072. }
  2073. cur += iosize;
  2074. pg_offset += iosize;
  2075. continue;
  2076. }
  2077. /* leave this out until we have a page_mkwrite call */
  2078. if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
  2079. EXTENT_DIRTY, 0, NULL)) {
  2080. cur = cur + iosize;
  2081. pg_offset += iosize;
  2082. continue;
  2083. }
  2084. if (tree->ops && tree->ops->writepage_io_hook) {
  2085. ret = tree->ops->writepage_io_hook(page, cur,
  2086. cur + iosize - 1);
  2087. } else {
  2088. ret = 0;
  2089. }
  2090. if (ret) {
  2091. SetPageError(page);
  2092. } else {
  2093. unsigned long max_nr = end_index + 1;
  2094. set_range_writeback(tree, cur, cur + iosize - 1);
  2095. if (!PageWriteback(page)) {
  2096. printk(KERN_ERR "btrfs warning page %lu not "
  2097. "writeback, cur %llu end %llu\n",
  2098. page->index, (unsigned long long)cur,
  2099. (unsigned long long)end);
  2100. }
  2101. ret = submit_extent_page(write_flags, tree, page,
  2102. sector, iosize, pg_offset,
  2103. bdev, &epd->bio, max_nr,
  2104. end_bio_extent_writepage,
  2105. 0, 0, 0);
  2106. if (ret)
  2107. SetPageError(page);
  2108. }
  2109. cur = cur + iosize;
  2110. pg_offset += iosize;
  2111. nr++;
  2112. }
  2113. done:
  2114. if (nr == 0) {
  2115. /* make sure the mapping tag for page dirty gets cleared */
  2116. set_page_writeback(page);
  2117. end_page_writeback(page);
  2118. }
  2119. unlock_page(page);
  2120. done_unlocked:
  2121. /* drop our reference on any cached states */
  2122. free_extent_state(cached_state);
  2123. return 0;
  2124. }
  2125. /**
  2126. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  2127. * @mapping: address space structure to write
  2128. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  2129. * @writepage: function called for each page
  2130. * @data: data passed to writepage function
  2131. *
  2132. * If a page is already under I/O, write_cache_pages() skips it, even
  2133. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  2134. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  2135. * and msync() need to guarantee that all the data which was dirty at the time
  2136. * the call was made get new I/O started against them. If wbc->sync_mode is
  2137. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  2138. * existing IO to complete.
  2139. */
  2140. static int extent_write_cache_pages(struct extent_io_tree *tree,
  2141. struct address_space *mapping,
  2142. struct writeback_control *wbc,
  2143. writepage_t writepage, void *data,
  2144. void (*flush_fn)(void *))
  2145. {
  2146. int ret = 0;
  2147. int done = 0;
  2148. int nr_to_write_done = 0;
  2149. struct pagevec pvec;
  2150. int nr_pages;
  2151. pgoff_t index;
  2152. pgoff_t end; /* Inclusive */
  2153. int scanned = 0;
  2154. int tag;
  2155. pagevec_init(&pvec, 0);
  2156. if (wbc->range_cyclic) {
  2157. index = mapping->writeback_index; /* Start from prev offset */
  2158. end = -1;
  2159. } else {
  2160. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2161. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2162. scanned = 1;
  2163. }
  2164. if (wbc->sync_mode == WB_SYNC_ALL)
  2165. tag = PAGECACHE_TAG_TOWRITE;
  2166. else
  2167. tag = PAGECACHE_TAG_DIRTY;
  2168. retry:
  2169. if (wbc->sync_mode == WB_SYNC_ALL)
  2170. tag_pages_for_writeback(mapping, index, end);
  2171. while (!done && !nr_to_write_done && (index <= end) &&
  2172. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2173. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  2174. unsigned i;
  2175. scanned = 1;
  2176. for (i = 0; i < nr_pages; i++) {
  2177. struct page *page = pvec.pages[i];
  2178. /*
  2179. * At this point we hold neither mapping->tree_lock nor
  2180. * lock on the page itself: the page may be truncated or
  2181. * invalidated (changing page->mapping to NULL), or even
  2182. * swizzled back from swapper_space to tmpfs file
  2183. * mapping
  2184. */
  2185. if (tree->ops && tree->ops->write_cache_pages_lock_hook)
  2186. tree->ops->write_cache_pages_lock_hook(page);
  2187. else
  2188. lock_page(page);
  2189. if (unlikely(page->mapping != mapping)) {
  2190. unlock_page(page);
  2191. continue;
  2192. }
  2193. if (!wbc->range_cyclic && page->index > end) {
  2194. done = 1;
  2195. unlock_page(page);
  2196. continue;
  2197. }
  2198. if (wbc->sync_mode != WB_SYNC_NONE) {
  2199. if (PageWriteback(page))
  2200. flush_fn(data);
  2201. wait_on_page_writeback(page);
  2202. }
  2203. if (PageWriteback(page) ||
  2204. !clear_page_dirty_for_io(page)) {
  2205. unlock_page(page);
  2206. continue;
  2207. }
  2208. ret = (*writepage)(page, wbc, data);
  2209. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  2210. unlock_page(page);
  2211. ret = 0;
  2212. }
  2213. if (ret)
  2214. done = 1;
  2215. /*
  2216. * the filesystem may choose to bump up nr_to_write.
  2217. * We have to make sure to honor the new nr_to_write
  2218. * at any time
  2219. */
  2220. nr_to_write_done = wbc->nr_to_write <= 0;
  2221. }
  2222. pagevec_release(&pvec);
  2223. cond_resched();
  2224. }
  2225. if (!scanned && !done) {
  2226. /*
  2227. * We hit the last page and there is more work to be done: wrap
  2228. * back to the start of the file
  2229. */
  2230. scanned = 1;
  2231. index = 0;
  2232. goto retry;
  2233. }
  2234. return ret;
  2235. }
  2236. static void flush_epd_write_bio(struct extent_page_data *epd)
  2237. {
  2238. if (epd->bio) {
  2239. if (epd->sync_io)
  2240. submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
  2241. else
  2242. submit_one_bio(WRITE, epd->bio, 0, 0);
  2243. epd->bio = NULL;
  2244. }
  2245. }
  2246. static noinline void flush_write_bio(void *data)
  2247. {
  2248. struct extent_page_data *epd = data;
  2249. flush_epd_write_bio(epd);
  2250. }
  2251. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  2252. get_extent_t *get_extent,
  2253. struct writeback_control *wbc)
  2254. {
  2255. int ret;
  2256. struct extent_page_data epd = {
  2257. .bio = NULL,
  2258. .tree = tree,
  2259. .get_extent = get_extent,
  2260. .extent_locked = 0,
  2261. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2262. };
  2263. ret = __extent_writepage(page, wbc, &epd);
  2264. flush_epd_write_bio(&epd);
  2265. return ret;
  2266. }
  2267. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  2268. u64 start, u64 end, get_extent_t *get_extent,
  2269. int mode)
  2270. {
  2271. int ret = 0;
  2272. struct address_space *mapping = inode->i_mapping;
  2273. struct page *page;
  2274. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  2275. PAGE_CACHE_SHIFT;
  2276. struct extent_page_data epd = {
  2277. .bio = NULL,
  2278. .tree = tree,
  2279. .get_extent = get_extent,
  2280. .extent_locked = 1,
  2281. .sync_io = mode == WB_SYNC_ALL,
  2282. };
  2283. struct writeback_control wbc_writepages = {
  2284. .sync_mode = mode,
  2285. .nr_to_write = nr_pages * 2,
  2286. .range_start = start,
  2287. .range_end = end + 1,
  2288. };
  2289. while (start <= end) {
  2290. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  2291. if (clear_page_dirty_for_io(page))
  2292. ret = __extent_writepage(page, &wbc_writepages, &epd);
  2293. else {
  2294. if (tree->ops && tree->ops->writepage_end_io_hook)
  2295. tree->ops->writepage_end_io_hook(page, start,
  2296. start + PAGE_CACHE_SIZE - 1,
  2297. NULL, 1);
  2298. unlock_page(page);
  2299. }
  2300. page_cache_release(page);
  2301. start += PAGE_CACHE_SIZE;
  2302. }
  2303. flush_epd_write_bio(&epd);
  2304. return ret;
  2305. }
  2306. int extent_writepages(struct extent_io_tree *tree,
  2307. struct address_space *mapping,
  2308. get_extent_t *get_extent,
  2309. struct writeback_control *wbc)
  2310. {
  2311. int ret = 0;
  2312. struct extent_page_data epd = {
  2313. .bio = NULL,
  2314. .tree = tree,
  2315. .get_extent = get_extent,
  2316. .extent_locked = 0,
  2317. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  2318. };
  2319. ret = extent_write_cache_pages(tree, mapping, wbc,
  2320. __extent_writepage, &epd,
  2321. flush_write_bio);
  2322. flush_epd_write_bio(&epd);
  2323. return ret;
  2324. }
  2325. int extent_readpages(struct extent_io_tree *tree,
  2326. struct address_space *mapping,
  2327. struct list_head *pages, unsigned nr_pages,
  2328. get_extent_t get_extent)
  2329. {
  2330. struct bio *bio = NULL;
  2331. unsigned page_idx;
  2332. unsigned long bio_flags = 0;
  2333. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  2334. struct page *page = list_entry(pages->prev, struct page, lru);
  2335. prefetchw(&page->flags);
  2336. list_del(&page->lru);
  2337. if (!add_to_page_cache_lru(page, mapping,
  2338. page->index, GFP_NOFS)) {
  2339. __extent_read_full_page(tree, page, get_extent,
  2340. &bio, 0, &bio_flags);
  2341. }
  2342. page_cache_release(page);
  2343. }
  2344. BUG_ON(!list_empty(pages));
  2345. if (bio)
  2346. submit_one_bio(READ, bio, 0, bio_flags);
  2347. return 0;
  2348. }
  2349. /*
  2350. * basic invalidatepage code, this waits on any locked or writeback
  2351. * ranges corresponding to the page, and then deletes any extent state
  2352. * records from the tree
  2353. */
  2354. int extent_invalidatepage(struct extent_io_tree *tree,
  2355. struct page *page, unsigned long offset)
  2356. {
  2357. struct extent_state *cached_state = NULL;
  2358. u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
  2359. u64 end = start + PAGE_CACHE_SIZE - 1;
  2360. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  2361. start += (offset + blocksize - 1) & ~(blocksize - 1);
  2362. if (start > end)
  2363. return 0;
  2364. lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
  2365. wait_on_page_writeback(page);
  2366. clear_extent_bit(tree, start, end,
  2367. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  2368. EXTENT_DO_ACCOUNTING,
  2369. 1, 1, &cached_state, GFP_NOFS);
  2370. return 0;
  2371. }
  2372. /*
  2373. * a helper for releasepage, this tests for areas of the page that
  2374. * are locked or under IO and drops the related state bits if it is safe
  2375. * to drop the page.
  2376. */
  2377. int try_release_extent_state(struct extent_map_tree *map,
  2378. struct extent_io_tree *tree, struct page *page,
  2379. gfp_t mask)
  2380. {
  2381. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2382. u64 end = start + PAGE_CACHE_SIZE - 1;
  2383. int ret = 1;
  2384. if (test_range_bit(tree, start, end,
  2385. EXTENT_IOBITS, 0, NULL))
  2386. ret = 0;
  2387. else {
  2388. if ((mask & GFP_NOFS) == GFP_NOFS)
  2389. mask = GFP_NOFS;
  2390. /*
  2391. * at this point we can safely clear everything except the
  2392. * locked bit and the nodatasum bit
  2393. */
  2394. ret = clear_extent_bit(tree, start, end,
  2395. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  2396. 0, 0, NULL, mask);
  2397. /* if clear_extent_bit failed for enomem reasons,
  2398. * we can't allow the release to continue.
  2399. */
  2400. if (ret < 0)
  2401. ret = 0;
  2402. else
  2403. ret = 1;
  2404. }
  2405. return ret;
  2406. }
  2407. /*
  2408. * a helper for releasepage. As long as there are no locked extents
  2409. * in the range corresponding to the page, both state records and extent
  2410. * map records are removed
  2411. */
  2412. int try_release_extent_mapping(struct extent_map_tree *map,
  2413. struct extent_io_tree *tree, struct page *page,
  2414. gfp_t mask)
  2415. {
  2416. struct extent_map *em;
  2417. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  2418. u64 end = start + PAGE_CACHE_SIZE - 1;
  2419. if ((mask & __GFP_WAIT) &&
  2420. page->mapping->host->i_size > 16 * 1024 * 1024) {
  2421. u64 len;
  2422. while (start <= end) {
  2423. len = end - start + 1;
  2424. write_lock(&map->lock);
  2425. em = lookup_extent_mapping(map, start, len);
  2426. if (IS_ERR_OR_NULL(em)) {
  2427. write_unlock(&map->lock);
  2428. break;
  2429. }
  2430. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  2431. em->start != start) {
  2432. write_unlock(&map->lock);
  2433. free_extent_map(em);
  2434. break;
  2435. }
  2436. if (!test_range_bit(tree, em->start,
  2437. extent_map_end(em) - 1,
  2438. EXTENT_LOCKED | EXTENT_WRITEBACK,
  2439. 0, NULL)) {
  2440. remove_extent_mapping(map, em);
  2441. /* once for the rb tree */
  2442. free_extent_map(em);
  2443. }
  2444. start = extent_map_end(em);
  2445. write_unlock(&map->lock);
  2446. /* once for us */
  2447. free_extent_map(em);
  2448. }
  2449. }
  2450. return try_release_extent_state(map, tree, page, mask);
  2451. }
  2452. /*
  2453. * helper function for fiemap, which doesn't want to see any holes.
  2454. * This maps until we find something past 'last'
  2455. */
  2456. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  2457. u64 offset,
  2458. u64 last,
  2459. get_extent_t *get_extent)
  2460. {
  2461. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  2462. struct extent_map *em;
  2463. u64 len;
  2464. if (offset >= last)
  2465. return NULL;
  2466. while(1) {
  2467. len = last - offset;
  2468. if (len == 0)
  2469. break;
  2470. len = (len + sectorsize - 1) & ~(sectorsize - 1);
  2471. em = get_extent(inode, NULL, 0, offset, len, 0);
  2472. if (IS_ERR_OR_NULL(em))
  2473. return em;
  2474. /* if this isn't a hole return it */
  2475. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  2476. em->block_start != EXTENT_MAP_HOLE) {
  2477. return em;
  2478. }
  2479. /* this is a hole, advance to the next extent */
  2480. offset = extent_map_end(em);
  2481. free_extent_map(em);
  2482. if (offset >= last)
  2483. break;
  2484. }
  2485. return NULL;
  2486. }
  2487. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  2488. __u64 start, __u64 len, get_extent_t *get_extent)
  2489. {
  2490. int ret = 0;
  2491. u64 off = start;
  2492. u64 max = start + len;
  2493. u32 flags = 0;
  2494. u32 found_type;
  2495. u64 last;
  2496. u64 last_for_get_extent = 0;
  2497. u64 disko = 0;
  2498. u64 isize = i_size_read(inode);
  2499. struct btrfs_key found_key;
  2500. struct extent_map *em = NULL;
  2501. struct extent_state *cached_state = NULL;
  2502. struct btrfs_path *path;
  2503. struct btrfs_file_extent_item *item;
  2504. int end = 0;
  2505. u64 em_start = 0;
  2506. u64 em_len = 0;
  2507. u64 em_end = 0;
  2508. unsigned long emflags;
  2509. if (len == 0)
  2510. return -EINVAL;
  2511. path = btrfs_alloc_path();
  2512. if (!path)
  2513. return -ENOMEM;
  2514. path->leave_spinning = 1;
  2515. /*
  2516. * lookup the last file extent. We're not using i_size here
  2517. * because there might be preallocation past i_size
  2518. */
  2519. ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
  2520. path, btrfs_ino(inode), -1, 0);
  2521. if (ret < 0) {
  2522. btrfs_free_path(path);
  2523. return ret;
  2524. }
  2525. WARN_ON(!ret);
  2526. path->slots[0]--;
  2527. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2528. struct btrfs_file_extent_item);
  2529. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  2530. found_type = btrfs_key_type(&found_key);
  2531. /* No extents, but there might be delalloc bits */
  2532. if (found_key.objectid != btrfs_ino(inode) ||
  2533. found_type != BTRFS_EXTENT_DATA_KEY) {
  2534. /* have to trust i_size as the end */
  2535. last = (u64)-1;
  2536. last_for_get_extent = isize;
  2537. } else {
  2538. /*
  2539. * remember the start of the last extent. There are a
  2540. * bunch of different factors that go into the length of the
  2541. * extent, so its much less complex to remember where it started
  2542. */
  2543. last = found_key.offset;
  2544. last_for_get_extent = last + 1;
  2545. }
  2546. btrfs_free_path(path);
  2547. /*
  2548. * we might have some extents allocated but more delalloc past those
  2549. * extents. so, we trust isize unless the start of the last extent is
  2550. * beyond isize
  2551. */
  2552. if (last < isize) {
  2553. last = (u64)-1;
  2554. last_for_get_extent = isize;
  2555. }
  2556. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
  2557. &cached_state, GFP_NOFS);
  2558. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  2559. get_extent);
  2560. if (!em)
  2561. goto out;
  2562. if (IS_ERR(em)) {
  2563. ret = PTR_ERR(em);
  2564. goto out;
  2565. }
  2566. while (!end) {
  2567. u64 offset_in_extent;
  2568. /* break if the extent we found is outside the range */
  2569. if (em->start >= max || extent_map_end(em) < off)
  2570. break;
  2571. /*
  2572. * get_extent may return an extent that starts before our
  2573. * requested range. We have to make sure the ranges
  2574. * we return to fiemap always move forward and don't
  2575. * overlap, so adjust the offsets here
  2576. */
  2577. em_start = max(em->start, off);
  2578. /*
  2579. * record the offset from the start of the extent
  2580. * for adjusting the disk offset below
  2581. */
  2582. offset_in_extent = em_start - em->start;
  2583. em_end = extent_map_end(em);
  2584. em_len = em_end - em_start;
  2585. emflags = em->flags;
  2586. disko = 0;
  2587. flags = 0;
  2588. /*
  2589. * bump off for our next call to get_extent
  2590. */
  2591. off = extent_map_end(em);
  2592. if (off >= max)
  2593. end = 1;
  2594. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  2595. end = 1;
  2596. flags |= FIEMAP_EXTENT_LAST;
  2597. } else if (em->block_start == EXTENT_MAP_INLINE) {
  2598. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  2599. FIEMAP_EXTENT_NOT_ALIGNED);
  2600. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  2601. flags |= (FIEMAP_EXTENT_DELALLOC |
  2602. FIEMAP_EXTENT_UNKNOWN);
  2603. } else {
  2604. disko = em->block_start + offset_in_extent;
  2605. }
  2606. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  2607. flags |= FIEMAP_EXTENT_ENCODED;
  2608. free_extent_map(em);
  2609. em = NULL;
  2610. if ((em_start >= last) || em_len == (u64)-1 ||
  2611. (last == (u64)-1 && isize <= em_end)) {
  2612. flags |= FIEMAP_EXTENT_LAST;
  2613. end = 1;
  2614. }
  2615. /* now scan forward to see if this is really the last extent. */
  2616. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  2617. get_extent);
  2618. if (IS_ERR(em)) {
  2619. ret = PTR_ERR(em);
  2620. goto out;
  2621. }
  2622. if (!em) {
  2623. flags |= FIEMAP_EXTENT_LAST;
  2624. end = 1;
  2625. }
  2626. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  2627. em_len, flags);
  2628. if (ret)
  2629. goto out_free;
  2630. }
  2631. out_free:
  2632. free_extent_map(em);
  2633. out:
  2634. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
  2635. &cached_state, GFP_NOFS);
  2636. return ret;
  2637. }
  2638. static inline struct page *extent_buffer_page(struct extent_buffer *eb,
  2639. unsigned long i)
  2640. {
  2641. struct page *p;
  2642. struct address_space *mapping;
  2643. if (i == 0)
  2644. return eb->first_page;
  2645. i += eb->start >> PAGE_CACHE_SHIFT;
  2646. mapping = eb->first_page->mapping;
  2647. if (!mapping)
  2648. return NULL;
  2649. /*
  2650. * extent_buffer_page is only called after pinning the page
  2651. * by increasing the reference count. So we know the page must
  2652. * be in the radix tree.
  2653. */
  2654. rcu_read_lock();
  2655. p = radix_tree_lookup(&mapping->page_tree, i);
  2656. rcu_read_unlock();
  2657. return p;
  2658. }
  2659. static inline unsigned long num_extent_pages(u64 start, u64 len)
  2660. {
  2661. return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
  2662. (start >> PAGE_CACHE_SHIFT);
  2663. }
  2664. static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
  2665. u64 start,
  2666. unsigned long len,
  2667. gfp_t mask)
  2668. {
  2669. struct extent_buffer *eb = NULL;
  2670. #if LEAK_DEBUG
  2671. unsigned long flags;
  2672. #endif
  2673. eb = kmem_cache_zalloc(extent_buffer_cache, mask);
  2674. if (eb == NULL)
  2675. return NULL;
  2676. eb->start = start;
  2677. eb->len = len;
  2678. rwlock_init(&eb->lock);
  2679. atomic_set(&eb->write_locks, 0);
  2680. atomic_set(&eb->read_locks, 0);
  2681. atomic_set(&eb->blocking_readers, 0);
  2682. atomic_set(&eb->blocking_writers, 0);
  2683. atomic_set(&eb->spinning_readers, 0);
  2684. atomic_set(&eb->spinning_writers, 0);
  2685. init_waitqueue_head(&eb->write_lock_wq);
  2686. init_waitqueue_head(&eb->read_lock_wq);
  2687. #if LEAK_DEBUG
  2688. spin_lock_irqsave(&leak_lock, flags);
  2689. list_add(&eb->leak_list, &buffers);
  2690. spin_unlock_irqrestore(&leak_lock, flags);
  2691. #endif
  2692. atomic_set(&eb->refs, 1);
  2693. return eb;
  2694. }
  2695. static void __free_extent_buffer(struct extent_buffer *eb)
  2696. {
  2697. #if LEAK_DEBUG
  2698. unsigned long flags;
  2699. spin_lock_irqsave(&leak_lock, flags);
  2700. list_del(&eb->leak_list);
  2701. spin_unlock_irqrestore(&leak_lock, flags);
  2702. #endif
  2703. kmem_cache_free(extent_buffer_cache, eb);
  2704. }
  2705. /*
  2706. * Helper for releasing extent buffer page.
  2707. */
  2708. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
  2709. unsigned long start_idx)
  2710. {
  2711. unsigned long index;
  2712. struct page *page;
  2713. if (!eb->first_page)
  2714. return;
  2715. index = num_extent_pages(eb->start, eb->len);
  2716. if (start_idx >= index)
  2717. return;
  2718. do {
  2719. index--;
  2720. page = extent_buffer_page(eb, index);
  2721. if (page)
  2722. page_cache_release(page);
  2723. } while (index != start_idx);
  2724. }
  2725. /*
  2726. * Helper for releasing the extent buffer.
  2727. */
  2728. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  2729. {
  2730. btrfs_release_extent_buffer_page(eb, 0);
  2731. __free_extent_buffer(eb);
  2732. }
  2733. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  2734. u64 start, unsigned long len,
  2735. struct page *page0)
  2736. {
  2737. unsigned long num_pages = num_extent_pages(start, len);
  2738. unsigned long i;
  2739. unsigned long index = start >> PAGE_CACHE_SHIFT;
  2740. struct extent_buffer *eb;
  2741. struct extent_buffer *exists = NULL;
  2742. struct page *p;
  2743. struct address_space *mapping = tree->mapping;
  2744. int uptodate = 1;
  2745. int ret;
  2746. rcu_read_lock();
  2747. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  2748. if (eb && atomic_inc_not_zero(&eb->refs)) {
  2749. rcu_read_unlock();
  2750. mark_page_accessed(eb->first_page);
  2751. return eb;
  2752. }
  2753. rcu_read_unlock();
  2754. eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
  2755. if (!eb)
  2756. return NULL;
  2757. if (page0) {
  2758. eb->first_page = page0;
  2759. i = 1;
  2760. index++;
  2761. page_cache_get(page0);
  2762. mark_page_accessed(page0);
  2763. set_page_extent_mapped(page0);
  2764. set_page_extent_head(page0, len);
  2765. uptodate = PageUptodate(page0);
  2766. } else {
  2767. i = 0;
  2768. }
  2769. for (; i < num_pages; i++, index++) {
  2770. p = find_or_create_page(mapping, index, GFP_NOFS);
  2771. if (!p) {
  2772. WARN_ON(1);
  2773. goto free_eb;
  2774. }
  2775. set_page_extent_mapped(p);
  2776. mark_page_accessed(p);
  2777. if (i == 0) {
  2778. eb->first_page = p;
  2779. set_page_extent_head(p, len);
  2780. } else {
  2781. set_page_private(p, EXTENT_PAGE_PRIVATE);
  2782. }
  2783. if (!PageUptodate(p))
  2784. uptodate = 0;
  2785. /*
  2786. * see below about how we avoid a nasty race with release page
  2787. * and why we unlock later
  2788. */
  2789. if (i != 0)
  2790. unlock_page(p);
  2791. }
  2792. if (uptodate)
  2793. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  2794. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  2795. if (ret)
  2796. goto free_eb;
  2797. spin_lock(&tree->buffer_lock);
  2798. ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
  2799. if (ret == -EEXIST) {
  2800. exists = radix_tree_lookup(&tree->buffer,
  2801. start >> PAGE_CACHE_SHIFT);
  2802. /* add one reference for the caller */
  2803. atomic_inc(&exists->refs);
  2804. spin_unlock(&tree->buffer_lock);
  2805. radix_tree_preload_end();
  2806. goto free_eb;
  2807. }
  2808. /* add one reference for the tree */
  2809. atomic_inc(&eb->refs);
  2810. spin_unlock(&tree->buffer_lock);
  2811. radix_tree_preload_end();
  2812. /*
  2813. * there is a race where release page may have
  2814. * tried to find this extent buffer in the radix
  2815. * but failed. It will tell the VM it is safe to
  2816. * reclaim the, and it will clear the page private bit.
  2817. * We must make sure to set the page private bit properly
  2818. * after the extent buffer is in the radix tree so
  2819. * it doesn't get lost
  2820. */
  2821. set_page_extent_mapped(eb->first_page);
  2822. set_page_extent_head(eb->first_page, eb->len);
  2823. if (!page0)
  2824. unlock_page(eb->first_page);
  2825. return eb;
  2826. free_eb:
  2827. if (eb->first_page && !page0)
  2828. unlock_page(eb->first_page);
  2829. if (!atomic_dec_and_test(&eb->refs))
  2830. return exists;
  2831. btrfs_release_extent_buffer(eb);
  2832. return exists;
  2833. }
  2834. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  2835. u64 start, unsigned long len)
  2836. {
  2837. struct extent_buffer *eb;
  2838. rcu_read_lock();
  2839. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  2840. if (eb && atomic_inc_not_zero(&eb->refs)) {
  2841. rcu_read_unlock();
  2842. mark_page_accessed(eb->first_page);
  2843. return eb;
  2844. }
  2845. rcu_read_unlock();
  2846. return NULL;
  2847. }
  2848. void free_extent_buffer(struct extent_buffer *eb)
  2849. {
  2850. if (!eb)
  2851. return;
  2852. if (!atomic_dec_and_test(&eb->refs))
  2853. return;
  2854. WARN_ON(1);
  2855. }
  2856. int clear_extent_buffer_dirty(struct extent_io_tree *tree,
  2857. struct extent_buffer *eb)
  2858. {
  2859. unsigned long i;
  2860. unsigned long num_pages;
  2861. struct page *page;
  2862. num_pages = num_extent_pages(eb->start, eb->len);
  2863. for (i = 0; i < num_pages; i++) {
  2864. page = extent_buffer_page(eb, i);
  2865. if (!PageDirty(page))
  2866. continue;
  2867. lock_page(page);
  2868. WARN_ON(!PagePrivate(page));
  2869. set_page_extent_mapped(page);
  2870. if (i == 0)
  2871. set_page_extent_head(page, eb->len);
  2872. clear_page_dirty_for_io(page);
  2873. spin_lock_irq(&page->mapping->tree_lock);
  2874. if (!PageDirty(page)) {
  2875. radix_tree_tag_clear(&page->mapping->page_tree,
  2876. page_index(page),
  2877. PAGECACHE_TAG_DIRTY);
  2878. }
  2879. spin_unlock_irq(&page->mapping->tree_lock);
  2880. unlock_page(page);
  2881. }
  2882. return 0;
  2883. }
  2884. int set_extent_buffer_dirty(struct extent_io_tree *tree,
  2885. struct extent_buffer *eb)
  2886. {
  2887. unsigned long i;
  2888. unsigned long num_pages;
  2889. int was_dirty = 0;
  2890. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  2891. num_pages = num_extent_pages(eb->start, eb->len);
  2892. for (i = 0; i < num_pages; i++)
  2893. __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
  2894. return was_dirty;
  2895. }
  2896. static int __eb_straddles_pages(u64 start, u64 len)
  2897. {
  2898. if (len < PAGE_CACHE_SIZE)
  2899. return 1;
  2900. if (start & (PAGE_CACHE_SIZE - 1))
  2901. return 1;
  2902. if ((start + len) & (PAGE_CACHE_SIZE - 1))
  2903. return 1;
  2904. return 0;
  2905. }
  2906. static int eb_straddles_pages(struct extent_buffer *eb)
  2907. {
  2908. return __eb_straddles_pages(eb->start, eb->len);
  2909. }
  2910. int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
  2911. struct extent_buffer *eb,
  2912. struct extent_state **cached_state)
  2913. {
  2914. unsigned long i;
  2915. struct page *page;
  2916. unsigned long num_pages;
  2917. num_pages = num_extent_pages(eb->start, eb->len);
  2918. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  2919. if (eb_straddles_pages(eb)) {
  2920. clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
  2921. cached_state, GFP_NOFS);
  2922. }
  2923. for (i = 0; i < num_pages; i++) {
  2924. page = extent_buffer_page(eb, i);
  2925. if (page)
  2926. ClearPageUptodate(page);
  2927. }
  2928. return 0;
  2929. }
  2930. int set_extent_buffer_uptodate(struct extent_io_tree *tree,
  2931. struct extent_buffer *eb)
  2932. {
  2933. unsigned long i;
  2934. struct page *page;
  2935. unsigned long num_pages;
  2936. num_pages = num_extent_pages(eb->start, eb->len);
  2937. if (eb_straddles_pages(eb)) {
  2938. set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
  2939. NULL, GFP_NOFS);
  2940. }
  2941. for (i = 0; i < num_pages; i++) {
  2942. page = extent_buffer_page(eb, i);
  2943. if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
  2944. ((i == num_pages - 1) &&
  2945. ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
  2946. check_page_uptodate(tree, page);
  2947. continue;
  2948. }
  2949. SetPageUptodate(page);
  2950. }
  2951. return 0;
  2952. }
  2953. int extent_range_uptodate(struct extent_io_tree *tree,
  2954. u64 start, u64 end)
  2955. {
  2956. struct page *page;
  2957. int ret;
  2958. int pg_uptodate = 1;
  2959. int uptodate;
  2960. unsigned long index;
  2961. if (__eb_straddles_pages(start, end - start + 1)) {
  2962. ret = test_range_bit(tree, start, end,
  2963. EXTENT_UPTODATE, 1, NULL);
  2964. if (ret)
  2965. return 1;
  2966. }
  2967. while (start <= end) {
  2968. index = start >> PAGE_CACHE_SHIFT;
  2969. page = find_get_page(tree->mapping, index);
  2970. uptodate = PageUptodate(page);
  2971. page_cache_release(page);
  2972. if (!uptodate) {
  2973. pg_uptodate = 0;
  2974. break;
  2975. }
  2976. start += PAGE_CACHE_SIZE;
  2977. }
  2978. return pg_uptodate;
  2979. }
  2980. int extent_buffer_uptodate(struct extent_io_tree *tree,
  2981. struct extent_buffer *eb,
  2982. struct extent_state *cached_state)
  2983. {
  2984. int ret = 0;
  2985. unsigned long num_pages;
  2986. unsigned long i;
  2987. struct page *page;
  2988. int pg_uptodate = 1;
  2989. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  2990. return 1;
  2991. if (eb_straddles_pages(eb)) {
  2992. ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
  2993. EXTENT_UPTODATE, 1, cached_state);
  2994. if (ret)
  2995. return ret;
  2996. }
  2997. num_pages = num_extent_pages(eb->start, eb->len);
  2998. for (i = 0; i < num_pages; i++) {
  2999. page = extent_buffer_page(eb, i);
  3000. if (!PageUptodate(page)) {
  3001. pg_uptodate = 0;
  3002. break;
  3003. }
  3004. }
  3005. return pg_uptodate;
  3006. }
  3007. int read_extent_buffer_pages(struct extent_io_tree *tree,
  3008. struct extent_buffer *eb,
  3009. u64 start, int wait,
  3010. get_extent_t *get_extent, int mirror_num)
  3011. {
  3012. unsigned long i;
  3013. unsigned long start_i;
  3014. struct page *page;
  3015. int err;
  3016. int ret = 0;
  3017. int locked_pages = 0;
  3018. int all_uptodate = 1;
  3019. int inc_all_pages = 0;
  3020. unsigned long num_pages;
  3021. struct bio *bio = NULL;
  3022. unsigned long bio_flags = 0;
  3023. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  3024. return 0;
  3025. if (eb_straddles_pages(eb)) {
  3026. if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
  3027. EXTENT_UPTODATE, 1, NULL)) {
  3028. return 0;
  3029. }
  3030. }
  3031. if (start) {
  3032. WARN_ON(start < eb->start);
  3033. start_i = (start >> PAGE_CACHE_SHIFT) -
  3034. (eb->start >> PAGE_CACHE_SHIFT);
  3035. } else {
  3036. start_i = 0;
  3037. }
  3038. num_pages = num_extent_pages(eb->start, eb->len);
  3039. for (i = start_i; i < num_pages; i++) {
  3040. page = extent_buffer_page(eb, i);
  3041. if (!wait) {
  3042. if (!trylock_page(page))
  3043. goto unlock_exit;
  3044. } else {
  3045. lock_page(page);
  3046. }
  3047. locked_pages++;
  3048. if (!PageUptodate(page))
  3049. all_uptodate = 0;
  3050. }
  3051. if (all_uptodate) {
  3052. if (start_i == 0)
  3053. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3054. goto unlock_exit;
  3055. }
  3056. for (i = start_i; i < num_pages; i++) {
  3057. page = extent_buffer_page(eb, i);
  3058. WARN_ON(!PagePrivate(page));
  3059. set_page_extent_mapped(page);
  3060. if (i == 0)
  3061. set_page_extent_head(page, eb->len);
  3062. if (inc_all_pages)
  3063. page_cache_get(page);
  3064. if (!PageUptodate(page)) {
  3065. if (start_i == 0)
  3066. inc_all_pages = 1;
  3067. ClearPageError(page);
  3068. err = __extent_read_full_page(tree, page,
  3069. get_extent, &bio,
  3070. mirror_num, &bio_flags);
  3071. if (err)
  3072. ret = err;
  3073. } else {
  3074. unlock_page(page);
  3075. }
  3076. }
  3077. if (bio)
  3078. submit_one_bio(READ, bio, mirror_num, bio_flags);
  3079. if (ret || !wait)
  3080. return ret;
  3081. for (i = start_i; i < num_pages; i++) {
  3082. page = extent_buffer_page(eb, i);
  3083. wait_on_page_locked(page);
  3084. if (!PageUptodate(page))
  3085. ret = -EIO;
  3086. }
  3087. if (!ret)
  3088. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  3089. return ret;
  3090. unlock_exit:
  3091. i = start_i;
  3092. while (locked_pages > 0) {
  3093. page = extent_buffer_page(eb, i);
  3094. i++;
  3095. unlock_page(page);
  3096. locked_pages--;
  3097. }
  3098. return ret;
  3099. }
  3100. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  3101. unsigned long start,
  3102. unsigned long len)
  3103. {
  3104. size_t cur;
  3105. size_t offset;
  3106. struct page *page;
  3107. char *kaddr;
  3108. char *dst = (char *)dstv;
  3109. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3110. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3111. WARN_ON(start > eb->len);
  3112. WARN_ON(start + len > eb->start + eb->len);
  3113. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3114. while (len > 0) {
  3115. page = extent_buffer_page(eb, i);
  3116. cur = min(len, (PAGE_CACHE_SIZE - offset));
  3117. kaddr = page_address(page);
  3118. memcpy(dst, kaddr + offset, cur);
  3119. dst += cur;
  3120. len -= cur;
  3121. offset = 0;
  3122. i++;
  3123. }
  3124. }
  3125. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  3126. unsigned long min_len, char **map,
  3127. unsigned long *map_start,
  3128. unsigned long *map_len)
  3129. {
  3130. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  3131. char *kaddr;
  3132. struct page *p;
  3133. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3134. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3135. unsigned long end_i = (start_offset + start + min_len - 1) >>
  3136. PAGE_CACHE_SHIFT;
  3137. if (i != end_i)
  3138. return -EINVAL;
  3139. if (i == 0) {
  3140. offset = start_offset;
  3141. *map_start = 0;
  3142. } else {
  3143. offset = 0;
  3144. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  3145. }
  3146. if (start + min_len > eb->len) {
  3147. printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  3148. "wanted %lu %lu\n", (unsigned long long)eb->start,
  3149. eb->len, start, min_len);
  3150. WARN_ON(1);
  3151. return -EINVAL;
  3152. }
  3153. p = extent_buffer_page(eb, i);
  3154. kaddr = page_address(p);
  3155. *map = kaddr + offset;
  3156. *map_len = PAGE_CACHE_SIZE - offset;
  3157. return 0;
  3158. }
  3159. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  3160. unsigned long start,
  3161. unsigned long len)
  3162. {
  3163. size_t cur;
  3164. size_t offset;
  3165. struct page *page;
  3166. char *kaddr;
  3167. char *ptr = (char *)ptrv;
  3168. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3169. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3170. int ret = 0;
  3171. WARN_ON(start > eb->len);
  3172. WARN_ON(start + len > eb->start + eb->len);
  3173. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3174. while (len > 0) {
  3175. page = extent_buffer_page(eb, i);
  3176. cur = min(len, (PAGE_CACHE_SIZE - offset));
  3177. kaddr = page_address(page);
  3178. ret = memcmp(ptr, kaddr + offset, cur);
  3179. if (ret)
  3180. break;
  3181. ptr += cur;
  3182. len -= cur;
  3183. offset = 0;
  3184. i++;
  3185. }
  3186. return ret;
  3187. }
  3188. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  3189. unsigned long start, unsigned long len)
  3190. {
  3191. size_t cur;
  3192. size_t offset;
  3193. struct page *page;
  3194. char *kaddr;
  3195. char *src = (char *)srcv;
  3196. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3197. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3198. WARN_ON(start > eb->len);
  3199. WARN_ON(start + len > eb->start + eb->len);
  3200. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3201. while (len > 0) {
  3202. page = extent_buffer_page(eb, i);
  3203. WARN_ON(!PageUptodate(page));
  3204. cur = min(len, PAGE_CACHE_SIZE - offset);
  3205. kaddr = page_address(page);
  3206. memcpy(kaddr + offset, src, cur);
  3207. src += cur;
  3208. len -= cur;
  3209. offset = 0;
  3210. i++;
  3211. }
  3212. }
  3213. void memset_extent_buffer(struct extent_buffer *eb, char c,
  3214. unsigned long start, unsigned long len)
  3215. {
  3216. size_t cur;
  3217. size_t offset;
  3218. struct page *page;
  3219. char *kaddr;
  3220. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  3221. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  3222. WARN_ON(start > eb->len);
  3223. WARN_ON(start + len > eb->start + eb->len);
  3224. offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
  3225. while (len > 0) {
  3226. page = extent_buffer_page(eb, i);
  3227. WARN_ON(!PageUptodate(page));
  3228. cur = min(len, PAGE_CACHE_SIZE - offset);
  3229. kaddr = page_address(page);
  3230. memset(kaddr + offset, c, cur);
  3231. len -= cur;
  3232. offset = 0;
  3233. i++;
  3234. }
  3235. }
  3236. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  3237. unsigned long dst_offset, unsigned long src_offset,
  3238. unsigned long len)
  3239. {
  3240. u64 dst_len = dst->len;
  3241. size_t cur;
  3242. size_t offset;
  3243. struct page *page;
  3244. char *kaddr;
  3245. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3246. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  3247. WARN_ON(src->len != dst_len);
  3248. offset = (start_offset + dst_offset) &
  3249. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3250. while (len > 0) {
  3251. page = extent_buffer_page(dst, i);
  3252. WARN_ON(!PageUptodate(page));
  3253. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  3254. kaddr = page_address(page);
  3255. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  3256. src_offset += cur;
  3257. len -= cur;
  3258. offset = 0;
  3259. i++;
  3260. }
  3261. }
  3262. static void move_pages(struct page *dst_page, struct page *src_page,
  3263. unsigned long dst_off, unsigned long src_off,
  3264. unsigned long len)
  3265. {
  3266. char *dst_kaddr = page_address(dst_page);
  3267. if (dst_page == src_page) {
  3268. memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
  3269. } else {
  3270. char *src_kaddr = page_address(src_page);
  3271. char *p = dst_kaddr + dst_off + len;
  3272. char *s = src_kaddr + src_off + len;
  3273. while (len--)
  3274. *--p = *--s;
  3275. }
  3276. }
  3277. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  3278. {
  3279. unsigned long distance = (src > dst) ? src - dst : dst - src;
  3280. return distance < len;
  3281. }
  3282. static void copy_pages(struct page *dst_page, struct page *src_page,
  3283. unsigned long dst_off, unsigned long src_off,
  3284. unsigned long len)
  3285. {
  3286. char *dst_kaddr = page_address(dst_page);
  3287. char *src_kaddr;
  3288. if (dst_page != src_page) {
  3289. src_kaddr = page_address(src_page);
  3290. } else {
  3291. src_kaddr = dst_kaddr;
  3292. BUG_ON(areas_overlap(src_off, dst_off, len));
  3293. }
  3294. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  3295. }
  3296. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  3297. unsigned long src_offset, unsigned long len)
  3298. {
  3299. size_t cur;
  3300. size_t dst_off_in_page;
  3301. size_t src_off_in_page;
  3302. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3303. unsigned long dst_i;
  3304. unsigned long src_i;
  3305. if (src_offset + len > dst->len) {
  3306. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  3307. "len %lu dst len %lu\n", src_offset, len, dst->len);
  3308. BUG_ON(1);
  3309. }
  3310. if (dst_offset + len > dst->len) {
  3311. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  3312. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  3313. BUG_ON(1);
  3314. }
  3315. while (len > 0) {
  3316. dst_off_in_page = (start_offset + dst_offset) &
  3317. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3318. src_off_in_page = (start_offset + src_offset) &
  3319. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3320. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  3321. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  3322. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  3323. src_off_in_page));
  3324. cur = min_t(unsigned long, cur,
  3325. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  3326. copy_pages(extent_buffer_page(dst, dst_i),
  3327. extent_buffer_page(dst, src_i),
  3328. dst_off_in_page, src_off_in_page, cur);
  3329. src_offset += cur;
  3330. dst_offset += cur;
  3331. len -= cur;
  3332. }
  3333. }
  3334. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  3335. unsigned long src_offset, unsigned long len)
  3336. {
  3337. size_t cur;
  3338. size_t dst_off_in_page;
  3339. size_t src_off_in_page;
  3340. unsigned long dst_end = dst_offset + len - 1;
  3341. unsigned long src_end = src_offset + len - 1;
  3342. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  3343. unsigned long dst_i;
  3344. unsigned long src_i;
  3345. if (src_offset + len > dst->len) {
  3346. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  3347. "len %lu len %lu\n", src_offset, len, dst->len);
  3348. BUG_ON(1);
  3349. }
  3350. if (dst_offset + len > dst->len) {
  3351. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  3352. "len %lu len %lu\n", dst_offset, len, dst->len);
  3353. BUG_ON(1);
  3354. }
  3355. if (!areas_overlap(src_offset, dst_offset, len)) {
  3356. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  3357. return;
  3358. }
  3359. while (len > 0) {
  3360. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  3361. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  3362. dst_off_in_page = (start_offset + dst_end) &
  3363. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3364. src_off_in_page = (start_offset + src_end) &
  3365. ((unsigned long)PAGE_CACHE_SIZE - 1);
  3366. cur = min_t(unsigned long, len, src_off_in_page + 1);
  3367. cur = min(cur, dst_off_in_page + 1);
  3368. move_pages(extent_buffer_page(dst, dst_i),
  3369. extent_buffer_page(dst, src_i),
  3370. dst_off_in_page - cur + 1,
  3371. src_off_in_page - cur + 1, cur);
  3372. dst_end -= cur;
  3373. src_end -= cur;
  3374. len -= cur;
  3375. }
  3376. }
  3377. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  3378. {
  3379. struct extent_buffer *eb =
  3380. container_of(head, struct extent_buffer, rcu_head);
  3381. btrfs_release_extent_buffer(eb);
  3382. }
  3383. int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
  3384. {
  3385. u64 start = page_offset(page);
  3386. struct extent_buffer *eb;
  3387. int ret = 1;
  3388. spin_lock(&tree->buffer_lock);
  3389. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3390. if (!eb) {
  3391. spin_unlock(&tree->buffer_lock);
  3392. return ret;
  3393. }
  3394. if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3395. ret = 0;
  3396. goto out;
  3397. }
  3398. /*
  3399. * set @eb->refs to 0 if it is already 1, and then release the @eb.
  3400. * Or go back.
  3401. */
  3402. if (atomic_cmpxchg(&eb->refs, 1, 0) != 1) {
  3403. ret = 0;
  3404. goto out;
  3405. }
  3406. radix_tree_delete(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  3407. out:
  3408. spin_unlock(&tree->buffer_lock);
  3409. /* at this point we can safely release the extent buffer */
  3410. if (atomic_read(&eb->refs) == 0)
  3411. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  3412. return ret;
  3413. }