extent_io.c 100 KB

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