extent_io.c 120 KB

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