extent_io.c 119 KB

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