extent_io.c 94 KB

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