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