extent_io.c 110 KB

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