extent_io.c 130 KB

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