extent_io.c 99 KB

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