extent_io.c 130 KB

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