extent_io.c 92 KB

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