extent_io.c 124 KB

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