extent_io.c 94 KB

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