extent_io.c 93 KB

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