extent_io.c 92 KB

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