extent_io.c 124 KB

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