extent_io.c 123 KB

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