extent_io.c 128 KB

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