extent_io.c 94 KB

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