extent_io.c 94 KB

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