extent_io.c 96 KB

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