extent_io.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242
  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. static void repair_io_failure_callback(struct bio *bio, int err)
  1751. {
  1752. complete(bio->bi_private);
  1753. }
  1754. /*
  1755. * this bypasses the standard btrfs submit functions deliberately, as
  1756. * the standard behavior is to write all copies in a raid setup. here we only
  1757. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1758. * submit_bio directly.
  1759. * to avoid any synchronization issues, wait for the data after writing, which
  1760. * actually prevents the read that triggered the error from finishing.
  1761. * currently, there can be no more than two copies of every data bit. thus,
  1762. * exactly one rewrite is required.
  1763. */
  1764. int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
  1765. u64 length, u64 logical, struct page *page,
  1766. int mirror_num)
  1767. {
  1768. struct bio *bio;
  1769. struct btrfs_device *dev;
  1770. DECLARE_COMPLETION_ONSTACK(compl);
  1771. u64 map_length = 0;
  1772. u64 sector;
  1773. struct btrfs_bio *bbio = NULL;
  1774. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  1775. int ret;
  1776. BUG_ON(!mirror_num);
  1777. /* we can't repair anything in raid56 yet */
  1778. if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
  1779. return 0;
  1780. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  1781. if (!bio)
  1782. return -EIO;
  1783. bio->bi_private = &compl;
  1784. bio->bi_end_io = repair_io_failure_callback;
  1785. bio->bi_size = 0;
  1786. map_length = length;
  1787. ret = btrfs_map_block(fs_info, WRITE, logical,
  1788. &map_length, &bbio, mirror_num);
  1789. if (ret) {
  1790. bio_put(bio);
  1791. return -EIO;
  1792. }
  1793. BUG_ON(mirror_num != bbio->mirror_num);
  1794. sector = bbio->stripes[mirror_num-1].physical >> 9;
  1795. bio->bi_sector = sector;
  1796. dev = bbio->stripes[mirror_num-1].dev;
  1797. kfree(bbio);
  1798. if (!dev || !dev->bdev || !dev->writeable) {
  1799. bio_put(bio);
  1800. return -EIO;
  1801. }
  1802. bio->bi_bdev = dev->bdev;
  1803. bio_add_page(bio, page, length, start - page_offset(page));
  1804. btrfsic_submit_bio(WRITE_SYNC, bio);
  1805. wait_for_completion(&compl);
  1806. if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  1807. /* try to remap that extent elsewhere? */
  1808. bio_put(bio);
  1809. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1810. return -EIO;
  1811. }
  1812. printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
  1813. "(dev %s sector %llu)\n", page->mapping->host->i_ino,
  1814. start, rcu_str_deref(dev->name), sector);
  1815. bio_put(bio);
  1816. return 0;
  1817. }
  1818. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  1819. int mirror_num)
  1820. {
  1821. u64 start = eb->start;
  1822. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1823. int ret = 0;
  1824. for (i = 0; i < num_pages; i++) {
  1825. struct page *p = extent_buffer_page(eb, i);
  1826. ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
  1827. start, p, mirror_num);
  1828. if (ret)
  1829. break;
  1830. start += PAGE_CACHE_SIZE;
  1831. }
  1832. return ret;
  1833. }
  1834. /*
  1835. * each time an IO finishes, we do a fast check in the IO failure tree
  1836. * to see if we need to process or clean up an io_failure_record
  1837. */
  1838. static int clean_io_failure(u64 start, struct page *page)
  1839. {
  1840. u64 private;
  1841. u64 private_failure;
  1842. struct io_failure_record *failrec;
  1843. struct btrfs_fs_info *fs_info;
  1844. struct extent_state *state;
  1845. int num_copies;
  1846. int did_repair = 0;
  1847. int ret;
  1848. struct inode *inode = page->mapping->host;
  1849. private = 0;
  1850. ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1851. (u64)-1, 1, EXTENT_DIRTY, 0);
  1852. if (!ret)
  1853. return 0;
  1854. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
  1855. &private_failure);
  1856. if (ret)
  1857. return 0;
  1858. failrec = (struct io_failure_record *)(unsigned long) private_failure;
  1859. BUG_ON(!failrec->this_mirror);
  1860. if (failrec->in_validation) {
  1861. /* there was no real error, just free the record */
  1862. pr_debug("clean_io_failure: freeing dummy error at %llu\n",
  1863. failrec->start);
  1864. did_repair = 1;
  1865. goto out;
  1866. }
  1867. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1868. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1869. failrec->start,
  1870. EXTENT_LOCKED);
  1871. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1872. if (state && state->start <= failrec->start &&
  1873. state->end >= failrec->start + failrec->len - 1) {
  1874. fs_info = BTRFS_I(inode)->root->fs_info;
  1875. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1876. failrec->len);
  1877. if (num_copies > 1) {
  1878. ret = repair_io_failure(fs_info, start, failrec->len,
  1879. failrec->logical, page,
  1880. failrec->failed_mirror);
  1881. did_repair = !ret;
  1882. }
  1883. ret = 0;
  1884. }
  1885. out:
  1886. if (!ret)
  1887. ret = free_io_failure(inode, failrec, did_repair);
  1888. return ret;
  1889. }
  1890. /*
  1891. * this is a generic handler for readpage errors (default
  1892. * readpage_io_failed_hook). if other copies exist, read those and write back
  1893. * good data to the failed position. does not investigate in remapping the
  1894. * failed extent elsewhere, hoping the device will be smart enough to do this as
  1895. * needed
  1896. */
  1897. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  1898. struct page *page, u64 start, u64 end,
  1899. int failed_mirror)
  1900. {
  1901. struct io_failure_record *failrec = NULL;
  1902. u64 private;
  1903. struct extent_map *em;
  1904. struct inode *inode = page->mapping->host;
  1905. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1906. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1907. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1908. struct bio *bio;
  1909. struct btrfs_io_bio *btrfs_failed_bio;
  1910. struct btrfs_io_bio *btrfs_bio;
  1911. int num_copies;
  1912. int ret;
  1913. int read_mode;
  1914. u64 logical;
  1915. BUG_ON(failed_bio->bi_rw & REQ_WRITE);
  1916. ret = get_state_private(failure_tree, start, &private);
  1917. if (ret) {
  1918. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1919. if (!failrec)
  1920. return -ENOMEM;
  1921. failrec->start = start;
  1922. failrec->len = end - start + 1;
  1923. failrec->this_mirror = 0;
  1924. failrec->bio_flags = 0;
  1925. failrec->in_validation = 0;
  1926. read_lock(&em_tree->lock);
  1927. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1928. if (!em) {
  1929. read_unlock(&em_tree->lock);
  1930. kfree(failrec);
  1931. return -EIO;
  1932. }
  1933. if (em->start > start || em->start + em->len < start) {
  1934. free_extent_map(em);
  1935. em = NULL;
  1936. }
  1937. read_unlock(&em_tree->lock);
  1938. if (!em) {
  1939. kfree(failrec);
  1940. return -EIO;
  1941. }
  1942. logical = start - em->start;
  1943. logical = em->block_start + logical;
  1944. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1945. logical = em->block_start;
  1946. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1947. extent_set_compress_type(&failrec->bio_flags,
  1948. em->compress_type);
  1949. }
  1950. pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
  1951. "len=%llu\n", logical, start, failrec->len);
  1952. failrec->logical = logical;
  1953. free_extent_map(em);
  1954. /* set the bits in the private failure tree */
  1955. ret = set_extent_bits(failure_tree, start, end,
  1956. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1957. if (ret >= 0)
  1958. ret = set_state_private(failure_tree, start,
  1959. (u64)(unsigned long)failrec);
  1960. /* set the bits in the inode's tree */
  1961. if (ret >= 0)
  1962. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
  1963. GFP_NOFS);
  1964. if (ret < 0) {
  1965. kfree(failrec);
  1966. return ret;
  1967. }
  1968. } else {
  1969. failrec = (struct io_failure_record *)(unsigned long)private;
  1970. pr_debug("bio_readpage_error: (found) logical=%llu, "
  1971. "start=%llu, len=%llu, validation=%d\n",
  1972. failrec->logical, failrec->start, failrec->len,
  1973. failrec->in_validation);
  1974. /*
  1975. * when data can be on disk more than twice, add to failrec here
  1976. * (e.g. with a list for failed_mirror) to make
  1977. * clean_io_failure() clean all those errors at once.
  1978. */
  1979. }
  1980. num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
  1981. failrec->logical, failrec->len);
  1982. if (num_copies == 1) {
  1983. /*
  1984. * we only have a single copy of the data, so don't bother with
  1985. * all the retry and error correction code that follows. no
  1986. * matter what the error is, it is very likely to persist.
  1987. */
  1988. pr_debug("bio_readpage_error: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
  1989. num_copies, failrec->this_mirror, failed_mirror);
  1990. free_io_failure(inode, failrec, 0);
  1991. return -EIO;
  1992. }
  1993. /*
  1994. * there are two premises:
  1995. * a) deliver good data to the caller
  1996. * b) correct the bad sectors on disk
  1997. */
  1998. if (failed_bio->bi_vcnt > 1) {
  1999. /*
  2000. * to fulfill b), we need to know the exact failing sectors, as
  2001. * we don't want to rewrite any more than the failed ones. thus,
  2002. * we need separate read requests for the failed bio
  2003. *
  2004. * if the following BUG_ON triggers, our validation request got
  2005. * merged. we need separate requests for our algorithm to work.
  2006. */
  2007. BUG_ON(failrec->in_validation);
  2008. failrec->in_validation = 1;
  2009. failrec->this_mirror = failed_mirror;
  2010. read_mode = READ_SYNC | REQ_FAILFAST_DEV;
  2011. } else {
  2012. /*
  2013. * we're ready to fulfill a) and b) alongside. get a good copy
  2014. * of the failed sector and if we succeed, we have setup
  2015. * everything for repair_io_failure to do the rest for us.
  2016. */
  2017. if (failrec->in_validation) {
  2018. BUG_ON(failrec->this_mirror != failed_mirror);
  2019. failrec->in_validation = 0;
  2020. failrec->this_mirror = 0;
  2021. }
  2022. failrec->failed_mirror = failed_mirror;
  2023. failrec->this_mirror++;
  2024. if (failrec->this_mirror == failed_mirror)
  2025. failrec->this_mirror++;
  2026. read_mode = READ_SYNC;
  2027. }
  2028. if (failrec->this_mirror > num_copies) {
  2029. pr_debug("bio_readpage_error: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
  2030. num_copies, failrec->this_mirror, failed_mirror);
  2031. free_io_failure(inode, failrec, 0);
  2032. return -EIO;
  2033. }
  2034. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  2035. if (!bio) {
  2036. free_io_failure(inode, failrec, 0);
  2037. return -EIO;
  2038. }
  2039. bio->bi_end_io = failed_bio->bi_end_io;
  2040. bio->bi_sector = failrec->logical >> 9;
  2041. bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  2042. bio->bi_size = 0;
  2043. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2044. if (btrfs_failed_bio->csum) {
  2045. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  2046. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2047. btrfs_bio = btrfs_io_bio(bio);
  2048. btrfs_bio->csum = btrfs_bio->csum_inline;
  2049. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2050. phy_offset *= csum_size;
  2051. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + phy_offset,
  2052. csum_size);
  2053. }
  2054. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  2055. pr_debug("bio_readpage_error: submitting new read[%#x] to "
  2056. "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
  2057. failrec->this_mirror, num_copies, failrec->in_validation);
  2058. ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
  2059. failrec->this_mirror,
  2060. failrec->bio_flags, 0);
  2061. return ret;
  2062. }
  2063. /* lots and lots of room for performance fixes in the end_bio funcs */
  2064. int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2065. {
  2066. int uptodate = (err == 0);
  2067. struct extent_io_tree *tree;
  2068. int ret;
  2069. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2070. if (tree->ops && tree->ops->writepage_end_io_hook) {
  2071. ret = tree->ops->writepage_end_io_hook(page, start,
  2072. end, NULL, uptodate);
  2073. if (ret)
  2074. uptodate = 0;
  2075. }
  2076. if (!uptodate) {
  2077. ClearPageUptodate(page);
  2078. SetPageError(page);
  2079. }
  2080. return 0;
  2081. }
  2082. /*
  2083. * after a writepage IO is done, we need to:
  2084. * clear the uptodate bits on error
  2085. * clear the writeback bits in the extent tree for this IO
  2086. * end_page_writeback if the page has no more pending IO
  2087. *
  2088. * Scheduling is not allowed, so the extent state tree is expected
  2089. * to have one and only one object corresponding to this IO.
  2090. */
  2091. static void end_bio_extent_writepage(struct bio *bio, int err)
  2092. {
  2093. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2094. struct extent_io_tree *tree;
  2095. u64 start;
  2096. u64 end;
  2097. do {
  2098. struct page *page = bvec->bv_page;
  2099. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2100. /* We always issue full-page reads, but if some block
  2101. * in a page fails to read, blk_update_request() will
  2102. * advance bv_offset and adjust bv_len to compensate.
  2103. * Print a warning for nonzero offsets, and an error
  2104. * if they don't add up to a full page. */
  2105. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
  2106. printk("%s page write in btrfs with offset %u and length %u\n",
  2107. bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
  2108. ? KERN_ERR "partial" : KERN_INFO "incomplete",
  2109. bvec->bv_offset, bvec->bv_len);
  2110. start = page_offset(page);
  2111. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2112. if (--bvec >= bio->bi_io_vec)
  2113. prefetchw(&bvec->bv_page->flags);
  2114. if (end_extent_writepage(page, err, start, end))
  2115. continue;
  2116. end_page_writeback(page);
  2117. } while (bvec >= bio->bi_io_vec);
  2118. bio_put(bio);
  2119. }
  2120. static void
  2121. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2122. int uptodate)
  2123. {
  2124. struct extent_state *cached = NULL;
  2125. u64 end = start + len - 1;
  2126. if (uptodate && tree->track_uptodate)
  2127. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2128. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2129. }
  2130. /*
  2131. * after a readpage IO is done, we need to:
  2132. * clear the uptodate bits on error
  2133. * set the uptodate bits if things worked
  2134. * set the page up to date if all extents in the tree are uptodate
  2135. * clear the lock bit in the extent tree
  2136. * unlock the page if there are no other extents locked for it
  2137. *
  2138. * Scheduling is not allowed, so the extent state tree is expected
  2139. * to have one and only one object corresponding to this IO.
  2140. */
  2141. static void end_bio_extent_readpage(struct bio *bio, int err)
  2142. {
  2143. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  2144. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  2145. struct bio_vec *bvec = bio->bi_io_vec;
  2146. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2147. struct extent_io_tree *tree;
  2148. u64 offset = 0;
  2149. u64 start;
  2150. u64 end;
  2151. u64 len;
  2152. u64 extent_start = 0;
  2153. u64 extent_len = 0;
  2154. int mirror;
  2155. int ret;
  2156. if (err)
  2157. uptodate = 0;
  2158. do {
  2159. struct page *page = bvec->bv_page;
  2160. struct inode *inode = page->mapping->host;
  2161. pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
  2162. "mirror=%lu\n", (u64)bio->bi_sector, err,
  2163. io_bio->mirror_num);
  2164. tree = &BTRFS_I(inode)->io_tree;
  2165. /* We always issue full-page reads, but if some block
  2166. * in a page fails to read, blk_update_request() will
  2167. * advance bv_offset and adjust bv_len to compensate.
  2168. * Print a warning for nonzero offsets, and an error
  2169. * if they don't add up to a full page. */
  2170. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
  2171. printk("%s page read in btrfs with offset %u and length %u\n",
  2172. bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
  2173. ? KERN_ERR "partial" : KERN_INFO "incomplete",
  2174. bvec->bv_offset, bvec->bv_len);
  2175. start = page_offset(page);
  2176. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2177. len = bvec->bv_len;
  2178. if (++bvec <= bvec_end)
  2179. prefetchw(&bvec->bv_page->flags);
  2180. mirror = io_bio->mirror_num;
  2181. if (likely(uptodate && tree->ops &&
  2182. tree->ops->readpage_end_io_hook)) {
  2183. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2184. page, start, end,
  2185. mirror);
  2186. if (ret)
  2187. uptodate = 0;
  2188. else
  2189. clean_io_failure(start, page);
  2190. }
  2191. if (likely(uptodate))
  2192. goto readpage_ok;
  2193. if (tree->ops && tree->ops->readpage_io_failed_hook) {
  2194. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2195. if (!ret && !err &&
  2196. test_bit(BIO_UPTODATE, &bio->bi_flags))
  2197. uptodate = 1;
  2198. } else {
  2199. /*
  2200. * The generic bio_readpage_error handles errors the
  2201. * following way: If possible, new read requests are
  2202. * created and submitted and will end up in
  2203. * end_bio_extent_readpage as well (if we're lucky, not
  2204. * in the !uptodate case). In that case it returns 0 and
  2205. * we just go on with the next page in our bio. If it
  2206. * can't handle the error it will return -EIO and we
  2207. * remain responsible for that page.
  2208. */
  2209. ret = bio_readpage_error(bio, offset, page, start, end,
  2210. mirror);
  2211. if (ret == 0) {
  2212. uptodate =
  2213. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2214. if (err)
  2215. uptodate = 0;
  2216. continue;
  2217. }
  2218. }
  2219. readpage_ok:
  2220. if (likely(uptodate)) {
  2221. loff_t i_size = i_size_read(inode);
  2222. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  2223. unsigned offset;
  2224. /* Zero out the end if this page straddles i_size */
  2225. offset = i_size & (PAGE_CACHE_SIZE-1);
  2226. if (page->index == end_index && offset)
  2227. zero_user_segment(page, offset, PAGE_CACHE_SIZE);
  2228. SetPageUptodate(page);
  2229. } else {
  2230. ClearPageUptodate(page);
  2231. SetPageError(page);
  2232. }
  2233. unlock_page(page);
  2234. offset += len;
  2235. if (unlikely(!uptodate)) {
  2236. if (extent_len) {
  2237. endio_readpage_release_extent(tree,
  2238. extent_start,
  2239. extent_len, 1);
  2240. extent_start = 0;
  2241. extent_len = 0;
  2242. }
  2243. endio_readpage_release_extent(tree, start,
  2244. end - start + 1, 0);
  2245. } else if (!extent_len) {
  2246. extent_start = start;
  2247. extent_len = end + 1 - start;
  2248. } else if (extent_start + extent_len == start) {
  2249. extent_len += end + 1 - start;
  2250. } else {
  2251. endio_readpage_release_extent(tree, extent_start,
  2252. extent_len, uptodate);
  2253. extent_start = start;
  2254. extent_len = end + 1 - start;
  2255. }
  2256. } while (bvec <= bvec_end);
  2257. if (extent_len)
  2258. endio_readpage_release_extent(tree, extent_start, extent_len,
  2259. uptodate);
  2260. if (io_bio->end_io)
  2261. io_bio->end_io(io_bio, err);
  2262. bio_put(bio);
  2263. }
  2264. /*
  2265. * this allocates from the btrfs_bioset. We're returning a bio right now
  2266. * but you can call btrfs_io_bio for the appropriate container_of magic
  2267. */
  2268. struct bio *
  2269. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2270. gfp_t gfp_flags)
  2271. {
  2272. struct btrfs_io_bio *btrfs_bio;
  2273. struct bio *bio;
  2274. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2275. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2276. while (!bio && (nr_vecs /= 2)) {
  2277. bio = bio_alloc_bioset(gfp_flags,
  2278. nr_vecs, btrfs_bioset);
  2279. }
  2280. }
  2281. if (bio) {
  2282. bio->bi_size = 0;
  2283. bio->bi_bdev = bdev;
  2284. bio->bi_sector = first_sector;
  2285. btrfs_bio = btrfs_io_bio(bio);
  2286. btrfs_bio->csum = NULL;
  2287. btrfs_bio->csum_allocated = NULL;
  2288. btrfs_bio->end_io = NULL;
  2289. }
  2290. return bio;
  2291. }
  2292. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2293. {
  2294. return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
  2295. }
  2296. /* this also allocates from the btrfs_bioset */
  2297. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2298. {
  2299. struct btrfs_io_bio *btrfs_bio;
  2300. struct bio *bio;
  2301. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2302. if (bio) {
  2303. btrfs_bio = btrfs_io_bio(bio);
  2304. btrfs_bio->csum = NULL;
  2305. btrfs_bio->csum_allocated = NULL;
  2306. btrfs_bio->end_io = NULL;
  2307. }
  2308. return bio;
  2309. }
  2310. static int __must_check submit_one_bio(int rw, struct bio *bio,
  2311. int mirror_num, unsigned long bio_flags)
  2312. {
  2313. int ret = 0;
  2314. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2315. struct page *page = bvec->bv_page;
  2316. struct extent_io_tree *tree = bio->bi_private;
  2317. u64 start;
  2318. start = page_offset(page) + bvec->bv_offset;
  2319. bio->bi_private = NULL;
  2320. bio_get(bio);
  2321. if (tree->ops && tree->ops->submit_bio_hook)
  2322. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2323. mirror_num, bio_flags, start);
  2324. else
  2325. btrfsic_submit_bio(rw, bio);
  2326. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  2327. ret = -EOPNOTSUPP;
  2328. bio_put(bio);
  2329. return ret;
  2330. }
  2331. static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
  2332. unsigned long offset, size_t size, struct bio *bio,
  2333. unsigned long bio_flags)
  2334. {
  2335. int ret = 0;
  2336. if (tree->ops && tree->ops->merge_bio_hook)
  2337. ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
  2338. bio_flags);
  2339. BUG_ON(ret < 0);
  2340. return ret;
  2341. }
  2342. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  2343. struct page *page, sector_t sector,
  2344. size_t size, unsigned long offset,
  2345. struct block_device *bdev,
  2346. struct bio **bio_ret,
  2347. unsigned long max_pages,
  2348. bio_end_io_t end_io_func,
  2349. int mirror_num,
  2350. unsigned long prev_bio_flags,
  2351. unsigned long bio_flags)
  2352. {
  2353. int ret = 0;
  2354. struct bio *bio;
  2355. int nr;
  2356. int contig = 0;
  2357. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2358. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2359. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2360. if (bio_ret && *bio_ret) {
  2361. bio = *bio_ret;
  2362. if (old_compressed)
  2363. contig = bio->bi_sector == sector;
  2364. else
  2365. contig = bio_end_sector(bio) == sector;
  2366. if (prev_bio_flags != bio_flags || !contig ||
  2367. merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
  2368. bio_add_page(bio, page, page_size, offset) < page_size) {
  2369. ret = submit_one_bio(rw, bio, mirror_num,
  2370. prev_bio_flags);
  2371. if (ret < 0)
  2372. return ret;
  2373. bio = NULL;
  2374. } else {
  2375. return 0;
  2376. }
  2377. }
  2378. if (this_compressed)
  2379. nr = BIO_MAX_PAGES;
  2380. else
  2381. nr = bio_get_nr_vecs(bdev);
  2382. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2383. if (!bio)
  2384. return -ENOMEM;
  2385. bio_add_page(bio, page, page_size, offset);
  2386. bio->bi_end_io = end_io_func;
  2387. bio->bi_private = tree;
  2388. if (bio_ret)
  2389. *bio_ret = bio;
  2390. else
  2391. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2392. return ret;
  2393. }
  2394. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2395. struct page *page)
  2396. {
  2397. if (!PagePrivate(page)) {
  2398. SetPagePrivate(page);
  2399. page_cache_get(page);
  2400. set_page_private(page, (unsigned long)eb);
  2401. } else {
  2402. WARN_ON(page->private != (unsigned long)eb);
  2403. }
  2404. }
  2405. void set_page_extent_mapped(struct page *page)
  2406. {
  2407. if (!PagePrivate(page)) {
  2408. SetPagePrivate(page);
  2409. page_cache_get(page);
  2410. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2411. }
  2412. }
  2413. static struct extent_map *
  2414. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2415. u64 start, u64 len, get_extent_t *get_extent,
  2416. struct extent_map **em_cached)
  2417. {
  2418. struct extent_map *em;
  2419. if (em_cached && *em_cached) {
  2420. em = *em_cached;
  2421. if (em->in_tree && start >= em->start &&
  2422. start < extent_map_end(em)) {
  2423. atomic_inc(&em->refs);
  2424. return em;
  2425. }
  2426. free_extent_map(em);
  2427. *em_cached = NULL;
  2428. }
  2429. em = get_extent(inode, page, pg_offset, start, len, 0);
  2430. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2431. BUG_ON(*em_cached);
  2432. atomic_inc(&em->refs);
  2433. *em_cached = em;
  2434. }
  2435. return em;
  2436. }
  2437. /*
  2438. * basic readpage implementation. Locked extent state structs are inserted
  2439. * into the tree that are removed when the IO is done (by the end_io
  2440. * handlers)
  2441. * XXX JDM: This needs looking at to ensure proper page locking
  2442. */
  2443. static int __do_readpage(struct extent_io_tree *tree,
  2444. struct page *page,
  2445. get_extent_t *get_extent,
  2446. struct extent_map **em_cached,
  2447. struct bio **bio, int mirror_num,
  2448. unsigned long *bio_flags, int rw)
  2449. {
  2450. struct inode *inode = page->mapping->host;
  2451. u64 start = page_offset(page);
  2452. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2453. u64 end;
  2454. u64 cur = start;
  2455. u64 extent_offset;
  2456. u64 last_byte = i_size_read(inode);
  2457. u64 block_start;
  2458. u64 cur_end;
  2459. sector_t sector;
  2460. struct extent_map *em;
  2461. struct block_device *bdev;
  2462. int ret;
  2463. int nr = 0;
  2464. int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
  2465. size_t pg_offset = 0;
  2466. size_t iosize;
  2467. size_t disk_io_size;
  2468. size_t blocksize = inode->i_sb->s_blocksize;
  2469. unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
  2470. set_page_extent_mapped(page);
  2471. end = page_end;
  2472. if (!PageUptodate(page)) {
  2473. if (cleancache_get_page(page) == 0) {
  2474. BUG_ON(blocksize != PAGE_SIZE);
  2475. unlock_extent(tree, start, end);
  2476. goto out;
  2477. }
  2478. }
  2479. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2480. char *userpage;
  2481. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2482. if (zero_offset) {
  2483. iosize = PAGE_CACHE_SIZE - zero_offset;
  2484. userpage = kmap_atomic(page);
  2485. memset(userpage + zero_offset, 0, iosize);
  2486. flush_dcache_page(page);
  2487. kunmap_atomic(userpage);
  2488. }
  2489. }
  2490. while (cur <= end) {
  2491. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2492. if (cur >= last_byte) {
  2493. char *userpage;
  2494. struct extent_state *cached = NULL;
  2495. iosize = PAGE_CACHE_SIZE - pg_offset;
  2496. userpage = kmap_atomic(page);
  2497. memset(userpage + pg_offset, 0, iosize);
  2498. flush_dcache_page(page);
  2499. kunmap_atomic(userpage);
  2500. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2501. &cached, GFP_NOFS);
  2502. if (!parent_locked)
  2503. unlock_extent_cached(tree, cur,
  2504. cur + iosize - 1,
  2505. &cached, GFP_NOFS);
  2506. break;
  2507. }
  2508. em = __get_extent_map(inode, page, pg_offset, cur,
  2509. end - cur + 1, get_extent, em_cached);
  2510. if (IS_ERR_OR_NULL(em)) {
  2511. SetPageError(page);
  2512. if (!parent_locked)
  2513. unlock_extent(tree, cur, end);
  2514. break;
  2515. }
  2516. extent_offset = cur - em->start;
  2517. BUG_ON(extent_map_end(em) <= cur);
  2518. BUG_ON(end < cur);
  2519. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2520. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2521. extent_set_compress_type(&this_bio_flag,
  2522. em->compress_type);
  2523. }
  2524. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2525. cur_end = min(extent_map_end(em) - 1, end);
  2526. iosize = ALIGN(iosize, blocksize);
  2527. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2528. disk_io_size = em->block_len;
  2529. sector = em->block_start >> 9;
  2530. } else {
  2531. sector = (em->block_start + extent_offset) >> 9;
  2532. disk_io_size = iosize;
  2533. }
  2534. bdev = em->bdev;
  2535. block_start = em->block_start;
  2536. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2537. block_start = EXTENT_MAP_HOLE;
  2538. free_extent_map(em);
  2539. em = NULL;
  2540. /* we've found a hole, just zero and go on */
  2541. if (block_start == EXTENT_MAP_HOLE) {
  2542. char *userpage;
  2543. struct extent_state *cached = NULL;
  2544. userpage = kmap_atomic(page);
  2545. memset(userpage + pg_offset, 0, iosize);
  2546. flush_dcache_page(page);
  2547. kunmap_atomic(userpage);
  2548. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2549. &cached, GFP_NOFS);
  2550. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2551. &cached, GFP_NOFS);
  2552. cur = cur + iosize;
  2553. pg_offset += iosize;
  2554. continue;
  2555. }
  2556. /* the get_extent function already copied into the page */
  2557. if (test_range_bit(tree, cur, cur_end,
  2558. EXTENT_UPTODATE, 1, NULL)) {
  2559. check_page_uptodate(tree, page);
  2560. if (!parent_locked)
  2561. unlock_extent(tree, cur, cur + iosize - 1);
  2562. cur = cur + iosize;
  2563. pg_offset += iosize;
  2564. continue;
  2565. }
  2566. /* we have an inline extent but it didn't get marked up
  2567. * to date. Error out
  2568. */
  2569. if (block_start == EXTENT_MAP_INLINE) {
  2570. SetPageError(page);
  2571. if (!parent_locked)
  2572. unlock_extent(tree, cur, cur + iosize - 1);
  2573. cur = cur + iosize;
  2574. pg_offset += iosize;
  2575. continue;
  2576. }
  2577. pnr -= page->index;
  2578. ret = submit_extent_page(rw, tree, page,
  2579. sector, disk_io_size, pg_offset,
  2580. bdev, bio, pnr,
  2581. end_bio_extent_readpage, mirror_num,
  2582. *bio_flags,
  2583. this_bio_flag);
  2584. if (!ret) {
  2585. nr++;
  2586. *bio_flags = this_bio_flag;
  2587. } else {
  2588. SetPageError(page);
  2589. if (!parent_locked)
  2590. unlock_extent(tree, cur, cur + iosize - 1);
  2591. }
  2592. cur = cur + iosize;
  2593. pg_offset += iosize;
  2594. }
  2595. out:
  2596. if (!nr) {
  2597. if (!PageError(page))
  2598. SetPageUptodate(page);
  2599. unlock_page(page);
  2600. }
  2601. return 0;
  2602. }
  2603. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2604. struct page *pages[], int nr_pages,
  2605. u64 start, u64 end,
  2606. get_extent_t *get_extent,
  2607. struct extent_map **em_cached,
  2608. struct bio **bio, int mirror_num,
  2609. unsigned long *bio_flags, int rw)
  2610. {
  2611. struct inode *inode;
  2612. struct btrfs_ordered_extent *ordered;
  2613. int index;
  2614. inode = pages[0]->mapping->host;
  2615. while (1) {
  2616. lock_extent(tree, start, end);
  2617. ordered = btrfs_lookup_ordered_range(inode, start,
  2618. end - start + 1);
  2619. if (!ordered)
  2620. break;
  2621. unlock_extent(tree, start, end);
  2622. btrfs_start_ordered_extent(inode, ordered, 1);
  2623. btrfs_put_ordered_extent(ordered);
  2624. }
  2625. for (index = 0; index < nr_pages; index++) {
  2626. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2627. mirror_num, bio_flags, rw);
  2628. page_cache_release(pages[index]);
  2629. }
  2630. }
  2631. static void __extent_readpages(struct extent_io_tree *tree,
  2632. struct page *pages[],
  2633. int nr_pages, get_extent_t *get_extent,
  2634. struct extent_map **em_cached,
  2635. struct bio **bio, int mirror_num,
  2636. unsigned long *bio_flags, int rw)
  2637. {
  2638. u64 start = 0;
  2639. u64 end = 0;
  2640. u64 page_start;
  2641. int index;
  2642. int first_index = 0;
  2643. for (index = 0; index < nr_pages; index++) {
  2644. page_start = page_offset(pages[index]);
  2645. if (!end) {
  2646. start = page_start;
  2647. end = start + PAGE_CACHE_SIZE - 1;
  2648. first_index = index;
  2649. } else if (end + 1 == page_start) {
  2650. end += PAGE_CACHE_SIZE;
  2651. } else {
  2652. __do_contiguous_readpages(tree, &pages[first_index],
  2653. index - first_index, start,
  2654. end, get_extent, em_cached,
  2655. bio, mirror_num, bio_flags,
  2656. rw);
  2657. start = page_start;
  2658. end = start + PAGE_CACHE_SIZE - 1;
  2659. first_index = index;
  2660. }
  2661. }
  2662. if (end)
  2663. __do_contiguous_readpages(tree, &pages[first_index],
  2664. index - first_index, start,
  2665. end, get_extent, em_cached, bio,
  2666. mirror_num, bio_flags, rw);
  2667. }
  2668. static int __extent_read_full_page(struct extent_io_tree *tree,
  2669. struct page *page,
  2670. get_extent_t *get_extent,
  2671. struct bio **bio, int mirror_num,
  2672. unsigned long *bio_flags, int rw)
  2673. {
  2674. struct inode *inode = page->mapping->host;
  2675. struct btrfs_ordered_extent *ordered;
  2676. u64 start = page_offset(page);
  2677. u64 end = start + PAGE_CACHE_SIZE - 1;
  2678. int ret;
  2679. while (1) {
  2680. lock_extent(tree, start, end);
  2681. ordered = btrfs_lookup_ordered_extent(inode, start);
  2682. if (!ordered)
  2683. break;
  2684. unlock_extent(tree, start, end);
  2685. btrfs_start_ordered_extent(inode, ordered, 1);
  2686. btrfs_put_ordered_extent(ordered);
  2687. }
  2688. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2689. bio_flags, rw);
  2690. return ret;
  2691. }
  2692. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2693. get_extent_t *get_extent, int mirror_num)
  2694. {
  2695. struct bio *bio = NULL;
  2696. unsigned long bio_flags = 0;
  2697. int ret;
  2698. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2699. &bio_flags, READ);
  2700. if (bio)
  2701. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2702. return ret;
  2703. }
  2704. int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
  2705. get_extent_t *get_extent, int mirror_num)
  2706. {
  2707. struct bio *bio = NULL;
  2708. unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED;
  2709. int ret;
  2710. ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num,
  2711. &bio_flags, READ);
  2712. if (bio)
  2713. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2714. return ret;
  2715. }
  2716. static noinline void update_nr_written(struct page *page,
  2717. struct writeback_control *wbc,
  2718. unsigned long nr_written)
  2719. {
  2720. wbc->nr_to_write -= nr_written;
  2721. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2722. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2723. page->mapping->writeback_index = page->index + nr_written;
  2724. }
  2725. /*
  2726. * the writepage semantics are similar to regular writepage. extent
  2727. * records are inserted to lock ranges in the tree, and as dirty areas
  2728. * are found, they are marked writeback. Then the lock bits are removed
  2729. * and the end_io handler clears the writeback ranges
  2730. */
  2731. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  2732. void *data)
  2733. {
  2734. struct inode *inode = page->mapping->host;
  2735. struct extent_page_data *epd = data;
  2736. struct extent_io_tree *tree = epd->tree;
  2737. u64 start = page_offset(page);
  2738. u64 delalloc_start;
  2739. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2740. u64 end;
  2741. u64 cur = start;
  2742. u64 extent_offset;
  2743. u64 last_byte = i_size_read(inode);
  2744. u64 block_start;
  2745. u64 iosize;
  2746. sector_t sector;
  2747. struct extent_state *cached_state = NULL;
  2748. struct extent_map *em;
  2749. struct block_device *bdev;
  2750. int ret;
  2751. int nr = 0;
  2752. size_t pg_offset = 0;
  2753. size_t blocksize;
  2754. loff_t i_size = i_size_read(inode);
  2755. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  2756. u64 nr_delalloc;
  2757. u64 delalloc_end;
  2758. int page_started;
  2759. int compressed;
  2760. int write_flags;
  2761. unsigned long nr_written = 0;
  2762. bool fill_delalloc = true;
  2763. if (wbc->sync_mode == WB_SYNC_ALL)
  2764. write_flags = WRITE_SYNC;
  2765. else
  2766. write_flags = WRITE;
  2767. trace___extent_writepage(page, inode, wbc);
  2768. WARN_ON(!PageLocked(page));
  2769. ClearPageError(page);
  2770. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  2771. if (page->index > end_index ||
  2772. (page->index == end_index && !pg_offset)) {
  2773. page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
  2774. unlock_page(page);
  2775. return 0;
  2776. }
  2777. if (page->index == end_index) {
  2778. char *userpage;
  2779. userpage = kmap_atomic(page);
  2780. memset(userpage + pg_offset, 0,
  2781. PAGE_CACHE_SIZE - pg_offset);
  2782. kunmap_atomic(userpage);
  2783. flush_dcache_page(page);
  2784. }
  2785. pg_offset = 0;
  2786. set_page_extent_mapped(page);
  2787. if (!tree->ops || !tree->ops->fill_delalloc)
  2788. fill_delalloc = false;
  2789. delalloc_start = start;
  2790. delalloc_end = 0;
  2791. page_started = 0;
  2792. if (!epd->extent_locked && fill_delalloc) {
  2793. u64 delalloc_to_write = 0;
  2794. /*
  2795. * make sure the wbc mapping index is at least updated
  2796. * to this page.
  2797. */
  2798. update_nr_written(page, wbc, 0);
  2799. while (delalloc_end < page_end) {
  2800. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2801. page,
  2802. &delalloc_start,
  2803. &delalloc_end,
  2804. 128 * 1024 * 1024);
  2805. if (nr_delalloc == 0) {
  2806. delalloc_start = delalloc_end + 1;
  2807. continue;
  2808. }
  2809. ret = tree->ops->fill_delalloc(inode, page,
  2810. delalloc_start,
  2811. delalloc_end,
  2812. &page_started,
  2813. &nr_written);
  2814. /* File system has been set read-only */
  2815. if (ret) {
  2816. SetPageError(page);
  2817. goto done;
  2818. }
  2819. /*
  2820. * delalloc_end is already one less than the total
  2821. * length, so we don't subtract one from
  2822. * PAGE_CACHE_SIZE
  2823. */
  2824. delalloc_to_write += (delalloc_end - delalloc_start +
  2825. PAGE_CACHE_SIZE) >>
  2826. PAGE_CACHE_SHIFT;
  2827. delalloc_start = delalloc_end + 1;
  2828. }
  2829. if (wbc->nr_to_write < delalloc_to_write) {
  2830. int thresh = 8192;
  2831. if (delalloc_to_write < thresh * 2)
  2832. thresh = delalloc_to_write;
  2833. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2834. thresh);
  2835. }
  2836. /* did the fill delalloc function already unlock and start
  2837. * the IO?
  2838. */
  2839. if (page_started) {
  2840. ret = 0;
  2841. /*
  2842. * we've unlocked the page, so we can't update
  2843. * the mapping's writeback index, just update
  2844. * nr_to_write.
  2845. */
  2846. wbc->nr_to_write -= nr_written;
  2847. goto done_unlocked;
  2848. }
  2849. }
  2850. if (tree->ops && tree->ops->writepage_start_hook) {
  2851. ret = tree->ops->writepage_start_hook(page, start,
  2852. page_end);
  2853. if (ret) {
  2854. /* Fixup worker will requeue */
  2855. if (ret == -EBUSY)
  2856. wbc->pages_skipped++;
  2857. else
  2858. redirty_page_for_writepage(wbc, page);
  2859. update_nr_written(page, wbc, nr_written);
  2860. unlock_page(page);
  2861. ret = 0;
  2862. goto done_unlocked;
  2863. }
  2864. }
  2865. /*
  2866. * we don't want to touch the inode after unlocking the page,
  2867. * so we update the mapping writeback index now
  2868. */
  2869. update_nr_written(page, wbc, nr_written + 1);
  2870. end = page_end;
  2871. if (last_byte <= start) {
  2872. if (tree->ops && tree->ops->writepage_end_io_hook)
  2873. tree->ops->writepage_end_io_hook(page, start,
  2874. page_end, NULL, 1);
  2875. goto done;
  2876. }
  2877. blocksize = inode->i_sb->s_blocksize;
  2878. while (cur <= end) {
  2879. if (cur >= last_byte) {
  2880. if (tree->ops && tree->ops->writepage_end_io_hook)
  2881. tree->ops->writepage_end_io_hook(page, cur,
  2882. page_end, NULL, 1);
  2883. break;
  2884. }
  2885. em = epd->get_extent(inode, page, pg_offset, cur,
  2886. end - cur + 1, 1);
  2887. if (IS_ERR_OR_NULL(em)) {
  2888. SetPageError(page);
  2889. break;
  2890. }
  2891. extent_offset = cur - em->start;
  2892. BUG_ON(extent_map_end(em) <= cur);
  2893. BUG_ON(end < cur);
  2894. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2895. iosize = ALIGN(iosize, blocksize);
  2896. sector = (em->block_start + extent_offset) >> 9;
  2897. bdev = em->bdev;
  2898. block_start = em->block_start;
  2899. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  2900. free_extent_map(em);
  2901. em = NULL;
  2902. /*
  2903. * compressed and inline extents are written through other
  2904. * paths in the FS
  2905. */
  2906. if (compressed || block_start == EXTENT_MAP_HOLE ||
  2907. block_start == EXTENT_MAP_INLINE) {
  2908. /*
  2909. * end_io notification does not happen here for
  2910. * compressed extents
  2911. */
  2912. if (!compressed && tree->ops &&
  2913. tree->ops->writepage_end_io_hook)
  2914. tree->ops->writepage_end_io_hook(page, cur,
  2915. cur + iosize - 1,
  2916. NULL, 1);
  2917. else if (compressed) {
  2918. /* we don't want to end_page_writeback on
  2919. * a compressed extent. this happens
  2920. * elsewhere
  2921. */
  2922. nr++;
  2923. }
  2924. cur += iosize;
  2925. pg_offset += iosize;
  2926. continue;
  2927. }
  2928. /* leave this out until we have a page_mkwrite call */
  2929. if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
  2930. EXTENT_DIRTY, 0, NULL)) {
  2931. cur = cur + iosize;
  2932. pg_offset += iosize;
  2933. continue;
  2934. }
  2935. if (tree->ops && tree->ops->writepage_io_hook) {
  2936. ret = tree->ops->writepage_io_hook(page, cur,
  2937. cur + iosize - 1);
  2938. } else {
  2939. ret = 0;
  2940. }
  2941. if (ret) {
  2942. SetPageError(page);
  2943. } else {
  2944. unsigned long max_nr = end_index + 1;
  2945. set_range_writeback(tree, cur, cur + iosize - 1);
  2946. if (!PageWriteback(page)) {
  2947. printk(KERN_ERR "btrfs warning page %lu not "
  2948. "writeback, cur %llu end %llu\n",
  2949. page->index, cur, end);
  2950. }
  2951. ret = submit_extent_page(write_flags, tree, page,
  2952. sector, iosize, pg_offset,
  2953. bdev, &epd->bio, max_nr,
  2954. end_bio_extent_writepage,
  2955. 0, 0, 0);
  2956. if (ret)
  2957. SetPageError(page);
  2958. }
  2959. cur = cur + iosize;
  2960. pg_offset += iosize;
  2961. nr++;
  2962. }
  2963. done:
  2964. if (nr == 0) {
  2965. /* make sure the mapping tag for page dirty gets cleared */
  2966. set_page_writeback(page);
  2967. end_page_writeback(page);
  2968. }
  2969. unlock_page(page);
  2970. done_unlocked:
  2971. /* drop our reference on any cached states */
  2972. free_extent_state(cached_state);
  2973. return 0;
  2974. }
  2975. static int eb_wait(void *word)
  2976. {
  2977. io_schedule();
  2978. return 0;
  2979. }
  2980. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  2981. {
  2982. wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
  2983. TASK_UNINTERRUPTIBLE);
  2984. }
  2985. static int lock_extent_buffer_for_io(struct extent_buffer *eb,
  2986. struct btrfs_fs_info *fs_info,
  2987. struct extent_page_data *epd)
  2988. {
  2989. unsigned long i, num_pages;
  2990. int flush = 0;
  2991. int ret = 0;
  2992. if (!btrfs_try_tree_write_lock(eb)) {
  2993. flush = 1;
  2994. flush_write_bio(epd);
  2995. btrfs_tree_lock(eb);
  2996. }
  2997. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  2998. btrfs_tree_unlock(eb);
  2999. if (!epd->sync_io)
  3000. return 0;
  3001. if (!flush) {
  3002. flush_write_bio(epd);
  3003. flush = 1;
  3004. }
  3005. while (1) {
  3006. wait_on_extent_buffer_writeback(eb);
  3007. btrfs_tree_lock(eb);
  3008. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3009. break;
  3010. btrfs_tree_unlock(eb);
  3011. }
  3012. }
  3013. /*
  3014. * We need to do this to prevent races in people who check if the eb is
  3015. * under IO since we can end up having no IO bits set for a short period
  3016. * of time.
  3017. */
  3018. spin_lock(&eb->refs_lock);
  3019. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3020. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3021. spin_unlock(&eb->refs_lock);
  3022. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3023. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3024. -eb->len,
  3025. fs_info->dirty_metadata_batch);
  3026. ret = 1;
  3027. } else {
  3028. spin_unlock(&eb->refs_lock);
  3029. }
  3030. btrfs_tree_unlock(eb);
  3031. if (!ret)
  3032. return ret;
  3033. num_pages = num_extent_pages(eb->start, eb->len);
  3034. for (i = 0; i < num_pages; i++) {
  3035. struct page *p = extent_buffer_page(eb, i);
  3036. if (!trylock_page(p)) {
  3037. if (!flush) {
  3038. flush_write_bio(epd);
  3039. flush = 1;
  3040. }
  3041. lock_page(p);
  3042. }
  3043. }
  3044. return ret;
  3045. }
  3046. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3047. {
  3048. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3049. smp_mb__after_clear_bit();
  3050. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3051. }
  3052. static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
  3053. {
  3054. int uptodate = err == 0;
  3055. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  3056. struct extent_buffer *eb;
  3057. int done;
  3058. do {
  3059. struct page *page = bvec->bv_page;
  3060. bvec--;
  3061. eb = (struct extent_buffer *)page->private;
  3062. BUG_ON(!eb);
  3063. done = atomic_dec_and_test(&eb->io_pages);
  3064. if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
  3065. set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  3066. ClearPageUptodate(page);
  3067. SetPageError(page);
  3068. }
  3069. end_page_writeback(page);
  3070. if (!done)
  3071. continue;
  3072. end_extent_buffer_writeback(eb);
  3073. } while (bvec >= bio->bi_io_vec);
  3074. bio_put(bio);
  3075. }
  3076. static int write_one_eb(struct extent_buffer *eb,
  3077. struct btrfs_fs_info *fs_info,
  3078. struct writeback_control *wbc,
  3079. struct extent_page_data *epd)
  3080. {
  3081. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3082. u64 offset = eb->start;
  3083. unsigned long i, num_pages;
  3084. unsigned long bio_flags = 0;
  3085. int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
  3086. int ret = 0;
  3087. clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  3088. num_pages = num_extent_pages(eb->start, eb->len);
  3089. atomic_set(&eb->io_pages, num_pages);
  3090. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3091. bio_flags = EXTENT_BIO_TREE_LOG;
  3092. for (i = 0; i < num_pages; i++) {
  3093. struct page *p = extent_buffer_page(eb, i);
  3094. clear_page_dirty_for_io(p);
  3095. set_page_writeback(p);
  3096. ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
  3097. PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
  3098. -1, end_bio_extent_buffer_writepage,
  3099. 0, epd->bio_flags, bio_flags);
  3100. epd->bio_flags = bio_flags;
  3101. if (ret) {
  3102. set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  3103. SetPageError(p);
  3104. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3105. end_extent_buffer_writeback(eb);
  3106. ret = -EIO;
  3107. break;
  3108. }
  3109. offset += PAGE_CACHE_SIZE;
  3110. update_nr_written(p, wbc, 1);
  3111. unlock_page(p);
  3112. }
  3113. if (unlikely(ret)) {
  3114. for (; i < num_pages; i++) {
  3115. struct page *p = extent_buffer_page(eb, i);
  3116. unlock_page(p);
  3117. }
  3118. }
  3119. return ret;
  3120. }
  3121. int btree_write_cache_pages(struct address_space *mapping,
  3122. struct writeback_control *wbc)
  3123. {
  3124. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3125. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3126. struct extent_buffer *eb, *prev_eb = NULL;
  3127. struct extent_page_data epd = {
  3128. .bio = NULL,
  3129. .tree = tree,
  3130. .extent_locked = 0,
  3131. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3132. .bio_flags = 0,
  3133. };
  3134. int ret = 0;
  3135. int done = 0;
  3136. int nr_to_write_done = 0;
  3137. struct pagevec pvec;
  3138. int nr_pages;
  3139. pgoff_t index;
  3140. pgoff_t end; /* Inclusive */
  3141. int scanned = 0;
  3142. int tag;
  3143. pagevec_init(&pvec, 0);
  3144. if (wbc->range_cyclic) {
  3145. index = mapping->writeback_index; /* Start from prev offset */
  3146. end = -1;
  3147. } else {
  3148. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3149. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3150. scanned = 1;
  3151. }
  3152. if (wbc->sync_mode == WB_SYNC_ALL)
  3153. tag = PAGECACHE_TAG_TOWRITE;
  3154. else
  3155. tag = PAGECACHE_TAG_DIRTY;
  3156. retry:
  3157. if (wbc->sync_mode == WB_SYNC_ALL)
  3158. tag_pages_for_writeback(mapping, index, end);
  3159. while (!done && !nr_to_write_done && (index <= end) &&
  3160. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3161. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3162. unsigned i;
  3163. scanned = 1;
  3164. for (i = 0; i < nr_pages; i++) {
  3165. struct page *page = pvec.pages[i];
  3166. if (!PagePrivate(page))
  3167. continue;
  3168. if (!wbc->range_cyclic && page->index > end) {
  3169. done = 1;
  3170. break;
  3171. }
  3172. spin_lock(&mapping->private_lock);
  3173. if (!PagePrivate(page)) {
  3174. spin_unlock(&mapping->private_lock);
  3175. continue;
  3176. }
  3177. eb = (struct extent_buffer *)page->private;
  3178. /*
  3179. * Shouldn't happen and normally this would be a BUG_ON
  3180. * but no sense in crashing the users box for something
  3181. * we can survive anyway.
  3182. */
  3183. if (WARN_ON(!eb)) {
  3184. spin_unlock(&mapping->private_lock);
  3185. continue;
  3186. }
  3187. if (eb == prev_eb) {
  3188. spin_unlock(&mapping->private_lock);
  3189. continue;
  3190. }
  3191. ret = atomic_inc_not_zero(&eb->refs);
  3192. spin_unlock(&mapping->private_lock);
  3193. if (!ret)
  3194. continue;
  3195. prev_eb = eb;
  3196. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3197. if (!ret) {
  3198. free_extent_buffer(eb);
  3199. continue;
  3200. }
  3201. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3202. if (ret) {
  3203. done = 1;
  3204. free_extent_buffer(eb);
  3205. break;
  3206. }
  3207. free_extent_buffer(eb);
  3208. /*
  3209. * the filesystem may choose to bump up nr_to_write.
  3210. * We have to make sure to honor the new nr_to_write
  3211. * at any time
  3212. */
  3213. nr_to_write_done = wbc->nr_to_write <= 0;
  3214. }
  3215. pagevec_release(&pvec);
  3216. cond_resched();
  3217. }
  3218. if (!scanned && !done) {
  3219. /*
  3220. * We hit the last page and there is more work to be done: wrap
  3221. * back to the start of the file
  3222. */
  3223. scanned = 1;
  3224. index = 0;
  3225. goto retry;
  3226. }
  3227. flush_write_bio(&epd);
  3228. return ret;
  3229. }
  3230. /**
  3231. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3232. * @mapping: address space structure to write
  3233. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3234. * @writepage: function called for each page
  3235. * @data: data passed to writepage function
  3236. *
  3237. * If a page is already under I/O, write_cache_pages() skips it, even
  3238. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3239. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3240. * and msync() need to guarantee that all the data which was dirty at the time
  3241. * the call was made get new I/O started against them. If wbc->sync_mode is
  3242. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3243. * existing IO to complete.
  3244. */
  3245. static int extent_write_cache_pages(struct extent_io_tree *tree,
  3246. struct address_space *mapping,
  3247. struct writeback_control *wbc,
  3248. writepage_t writepage, void *data,
  3249. void (*flush_fn)(void *))
  3250. {
  3251. struct inode *inode = mapping->host;
  3252. int ret = 0;
  3253. int done = 0;
  3254. int nr_to_write_done = 0;
  3255. struct pagevec pvec;
  3256. int nr_pages;
  3257. pgoff_t index;
  3258. pgoff_t end; /* Inclusive */
  3259. int scanned = 0;
  3260. int tag;
  3261. /*
  3262. * We have to hold onto the inode so that ordered extents can do their
  3263. * work when the IO finishes. The alternative to this is failing to add
  3264. * an ordered extent if the igrab() fails there and that is a huge pain
  3265. * to deal with, so instead just hold onto the inode throughout the
  3266. * writepages operation. If it fails here we are freeing up the inode
  3267. * anyway and we'd rather not waste our time writing out stuff that is
  3268. * going to be truncated anyway.
  3269. */
  3270. if (!igrab(inode))
  3271. return 0;
  3272. pagevec_init(&pvec, 0);
  3273. if (wbc->range_cyclic) {
  3274. index = mapping->writeback_index; /* Start from prev offset */
  3275. end = -1;
  3276. } else {
  3277. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3278. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3279. scanned = 1;
  3280. }
  3281. if (wbc->sync_mode == WB_SYNC_ALL)
  3282. tag = PAGECACHE_TAG_TOWRITE;
  3283. else
  3284. tag = PAGECACHE_TAG_DIRTY;
  3285. retry:
  3286. if (wbc->sync_mode == WB_SYNC_ALL)
  3287. tag_pages_for_writeback(mapping, index, end);
  3288. while (!done && !nr_to_write_done && (index <= end) &&
  3289. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3290. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3291. unsigned i;
  3292. scanned = 1;
  3293. for (i = 0; i < nr_pages; i++) {
  3294. struct page *page = pvec.pages[i];
  3295. /*
  3296. * At this point we hold neither mapping->tree_lock nor
  3297. * lock on the page itself: the page may be truncated or
  3298. * invalidated (changing page->mapping to NULL), or even
  3299. * swizzled back from swapper_space to tmpfs file
  3300. * mapping
  3301. */
  3302. if (!trylock_page(page)) {
  3303. flush_fn(data);
  3304. lock_page(page);
  3305. }
  3306. if (unlikely(page->mapping != mapping)) {
  3307. unlock_page(page);
  3308. continue;
  3309. }
  3310. if (!wbc->range_cyclic && page->index > end) {
  3311. done = 1;
  3312. unlock_page(page);
  3313. continue;
  3314. }
  3315. if (wbc->sync_mode != WB_SYNC_NONE) {
  3316. if (PageWriteback(page))
  3317. flush_fn(data);
  3318. wait_on_page_writeback(page);
  3319. }
  3320. if (PageWriteback(page) ||
  3321. !clear_page_dirty_for_io(page)) {
  3322. unlock_page(page);
  3323. continue;
  3324. }
  3325. ret = (*writepage)(page, wbc, data);
  3326. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3327. unlock_page(page);
  3328. ret = 0;
  3329. }
  3330. if (ret)
  3331. done = 1;
  3332. /*
  3333. * the filesystem may choose to bump up nr_to_write.
  3334. * We have to make sure to honor the new nr_to_write
  3335. * at any time
  3336. */
  3337. nr_to_write_done = wbc->nr_to_write <= 0;
  3338. }
  3339. pagevec_release(&pvec);
  3340. cond_resched();
  3341. }
  3342. if (!scanned && !done) {
  3343. /*
  3344. * We hit the last page and there is more work to be done: wrap
  3345. * back to the start of the file
  3346. */
  3347. scanned = 1;
  3348. index = 0;
  3349. goto retry;
  3350. }
  3351. btrfs_add_delayed_iput(inode);
  3352. return ret;
  3353. }
  3354. static void flush_epd_write_bio(struct extent_page_data *epd)
  3355. {
  3356. if (epd->bio) {
  3357. int rw = WRITE;
  3358. int ret;
  3359. if (epd->sync_io)
  3360. rw = WRITE_SYNC;
  3361. ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
  3362. BUG_ON(ret < 0); /* -ENOMEM */
  3363. epd->bio = NULL;
  3364. }
  3365. }
  3366. static noinline void flush_write_bio(void *data)
  3367. {
  3368. struct extent_page_data *epd = data;
  3369. flush_epd_write_bio(epd);
  3370. }
  3371. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3372. get_extent_t *get_extent,
  3373. struct writeback_control *wbc)
  3374. {
  3375. int ret;
  3376. struct extent_page_data epd = {
  3377. .bio = NULL,
  3378. .tree = tree,
  3379. .get_extent = get_extent,
  3380. .extent_locked = 0,
  3381. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3382. .bio_flags = 0,
  3383. };
  3384. ret = __extent_writepage(page, wbc, &epd);
  3385. flush_epd_write_bio(&epd);
  3386. return ret;
  3387. }
  3388. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3389. u64 start, u64 end, get_extent_t *get_extent,
  3390. int mode)
  3391. {
  3392. int ret = 0;
  3393. struct address_space *mapping = inode->i_mapping;
  3394. struct page *page;
  3395. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  3396. PAGE_CACHE_SHIFT;
  3397. struct extent_page_data epd = {
  3398. .bio = NULL,
  3399. .tree = tree,
  3400. .get_extent = get_extent,
  3401. .extent_locked = 1,
  3402. .sync_io = mode == WB_SYNC_ALL,
  3403. .bio_flags = 0,
  3404. };
  3405. struct writeback_control wbc_writepages = {
  3406. .sync_mode = mode,
  3407. .nr_to_write = nr_pages * 2,
  3408. .range_start = start,
  3409. .range_end = end + 1,
  3410. };
  3411. while (start <= end) {
  3412. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  3413. if (clear_page_dirty_for_io(page))
  3414. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3415. else {
  3416. if (tree->ops && tree->ops->writepage_end_io_hook)
  3417. tree->ops->writepage_end_io_hook(page, start,
  3418. start + PAGE_CACHE_SIZE - 1,
  3419. NULL, 1);
  3420. unlock_page(page);
  3421. }
  3422. page_cache_release(page);
  3423. start += PAGE_CACHE_SIZE;
  3424. }
  3425. flush_epd_write_bio(&epd);
  3426. return ret;
  3427. }
  3428. int extent_writepages(struct extent_io_tree *tree,
  3429. struct address_space *mapping,
  3430. get_extent_t *get_extent,
  3431. struct writeback_control *wbc)
  3432. {
  3433. int ret = 0;
  3434. struct extent_page_data epd = {
  3435. .bio = NULL,
  3436. .tree = tree,
  3437. .get_extent = get_extent,
  3438. .extent_locked = 0,
  3439. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3440. .bio_flags = 0,
  3441. };
  3442. ret = extent_write_cache_pages(tree, mapping, wbc,
  3443. __extent_writepage, &epd,
  3444. flush_write_bio);
  3445. flush_epd_write_bio(&epd);
  3446. return ret;
  3447. }
  3448. int extent_readpages(struct extent_io_tree *tree,
  3449. struct address_space *mapping,
  3450. struct list_head *pages, unsigned nr_pages,
  3451. get_extent_t get_extent)
  3452. {
  3453. struct bio *bio = NULL;
  3454. unsigned page_idx;
  3455. unsigned long bio_flags = 0;
  3456. struct page *pagepool[16];
  3457. struct page *page;
  3458. struct extent_map *em_cached = NULL;
  3459. int nr = 0;
  3460. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3461. page = list_entry(pages->prev, struct page, lru);
  3462. prefetchw(&page->flags);
  3463. list_del(&page->lru);
  3464. if (add_to_page_cache_lru(page, mapping,
  3465. page->index, GFP_NOFS)) {
  3466. page_cache_release(page);
  3467. continue;
  3468. }
  3469. pagepool[nr++] = page;
  3470. if (nr < ARRAY_SIZE(pagepool))
  3471. continue;
  3472. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3473. &bio, 0, &bio_flags, READ);
  3474. nr = 0;
  3475. }
  3476. if (nr)
  3477. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3478. &bio, 0, &bio_flags, READ);
  3479. if (em_cached)
  3480. free_extent_map(em_cached);
  3481. BUG_ON(!list_empty(pages));
  3482. if (bio)
  3483. return submit_one_bio(READ, bio, 0, bio_flags);
  3484. return 0;
  3485. }
  3486. /*
  3487. * basic invalidatepage code, this waits on any locked or writeback
  3488. * ranges corresponding to the page, and then deletes any extent state
  3489. * records from the tree
  3490. */
  3491. int extent_invalidatepage(struct extent_io_tree *tree,
  3492. struct page *page, unsigned long offset)
  3493. {
  3494. struct extent_state *cached_state = NULL;
  3495. u64 start = page_offset(page);
  3496. u64 end = start + PAGE_CACHE_SIZE - 1;
  3497. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3498. start += ALIGN(offset, blocksize);
  3499. if (start > end)
  3500. return 0;
  3501. lock_extent_bits(tree, start, end, 0, &cached_state);
  3502. wait_on_page_writeback(page);
  3503. clear_extent_bit(tree, start, end,
  3504. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3505. EXTENT_DO_ACCOUNTING,
  3506. 1, 1, &cached_state, GFP_NOFS);
  3507. return 0;
  3508. }
  3509. /*
  3510. * a helper for releasepage, this tests for areas of the page that
  3511. * are locked or under IO and drops the related state bits if it is safe
  3512. * to drop the page.
  3513. */
  3514. static int try_release_extent_state(struct extent_map_tree *map,
  3515. struct extent_io_tree *tree,
  3516. struct page *page, gfp_t mask)
  3517. {
  3518. u64 start = page_offset(page);
  3519. u64 end = start + PAGE_CACHE_SIZE - 1;
  3520. int ret = 1;
  3521. if (test_range_bit(tree, start, end,
  3522. EXTENT_IOBITS, 0, NULL))
  3523. ret = 0;
  3524. else {
  3525. if ((mask & GFP_NOFS) == GFP_NOFS)
  3526. mask = GFP_NOFS;
  3527. /*
  3528. * at this point we can safely clear everything except the
  3529. * locked bit and the nodatasum bit
  3530. */
  3531. ret = clear_extent_bit(tree, start, end,
  3532. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3533. 0, 0, NULL, mask);
  3534. /* if clear_extent_bit failed for enomem reasons,
  3535. * we can't allow the release to continue.
  3536. */
  3537. if (ret < 0)
  3538. ret = 0;
  3539. else
  3540. ret = 1;
  3541. }
  3542. return ret;
  3543. }
  3544. /*
  3545. * a helper for releasepage. As long as there are no locked extents
  3546. * in the range corresponding to the page, both state records and extent
  3547. * map records are removed
  3548. */
  3549. int try_release_extent_mapping(struct extent_map_tree *map,
  3550. struct extent_io_tree *tree, struct page *page,
  3551. gfp_t mask)
  3552. {
  3553. struct extent_map *em;
  3554. u64 start = page_offset(page);
  3555. u64 end = start + PAGE_CACHE_SIZE - 1;
  3556. if ((mask & __GFP_WAIT) &&
  3557. page->mapping->host->i_size > 16 * 1024 * 1024) {
  3558. u64 len;
  3559. while (start <= end) {
  3560. len = end - start + 1;
  3561. write_lock(&map->lock);
  3562. em = lookup_extent_mapping(map, start, len);
  3563. if (!em) {
  3564. write_unlock(&map->lock);
  3565. break;
  3566. }
  3567. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3568. em->start != start) {
  3569. write_unlock(&map->lock);
  3570. free_extent_map(em);
  3571. break;
  3572. }
  3573. if (!test_range_bit(tree, em->start,
  3574. extent_map_end(em) - 1,
  3575. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3576. 0, NULL)) {
  3577. remove_extent_mapping(map, em);
  3578. /* once for the rb tree */
  3579. free_extent_map(em);
  3580. }
  3581. start = extent_map_end(em);
  3582. write_unlock(&map->lock);
  3583. /* once for us */
  3584. free_extent_map(em);
  3585. }
  3586. }
  3587. return try_release_extent_state(map, tree, page, mask);
  3588. }
  3589. /*
  3590. * helper function for fiemap, which doesn't want to see any holes.
  3591. * This maps until we find something past 'last'
  3592. */
  3593. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3594. u64 offset,
  3595. u64 last,
  3596. get_extent_t *get_extent)
  3597. {
  3598. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3599. struct extent_map *em;
  3600. u64 len;
  3601. if (offset >= last)
  3602. return NULL;
  3603. while (1) {
  3604. len = last - offset;
  3605. if (len == 0)
  3606. break;
  3607. len = ALIGN(len, sectorsize);
  3608. em = get_extent(inode, NULL, 0, offset, len, 0);
  3609. if (IS_ERR_OR_NULL(em))
  3610. return em;
  3611. /* if this isn't a hole return it */
  3612. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3613. em->block_start != EXTENT_MAP_HOLE) {
  3614. return em;
  3615. }
  3616. /* this is a hole, advance to the next extent */
  3617. offset = extent_map_end(em);
  3618. free_extent_map(em);
  3619. if (offset >= last)
  3620. break;
  3621. }
  3622. return NULL;
  3623. }
  3624. static noinline int count_ext_ref(u64 inum, u64 offset, u64 root_id, void *ctx)
  3625. {
  3626. unsigned long cnt = *((unsigned long *)ctx);
  3627. cnt++;
  3628. *((unsigned long *)ctx) = cnt;
  3629. /* Now we're sure that the extent is shared. */
  3630. if (cnt > 1)
  3631. return 1;
  3632. return 0;
  3633. }
  3634. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3635. __u64 start, __u64 len, get_extent_t *get_extent)
  3636. {
  3637. int ret = 0;
  3638. u64 off = start;
  3639. u64 max = start + len;
  3640. u32 flags = 0;
  3641. u32 found_type;
  3642. u64 last;
  3643. u64 last_for_get_extent = 0;
  3644. u64 disko = 0;
  3645. u64 isize = i_size_read(inode);
  3646. struct btrfs_key found_key;
  3647. struct extent_map *em = NULL;
  3648. struct extent_state *cached_state = NULL;
  3649. struct btrfs_path *path;
  3650. struct btrfs_file_extent_item *item;
  3651. int end = 0;
  3652. u64 em_start = 0;
  3653. u64 em_len = 0;
  3654. u64 em_end = 0;
  3655. unsigned long emflags;
  3656. if (len == 0)
  3657. return -EINVAL;
  3658. path = btrfs_alloc_path();
  3659. if (!path)
  3660. return -ENOMEM;
  3661. path->leave_spinning = 1;
  3662. start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
  3663. len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
  3664. /*
  3665. * lookup the last file extent. We're not using i_size here
  3666. * because there might be preallocation past i_size
  3667. */
  3668. ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
  3669. path, btrfs_ino(inode), -1, 0);
  3670. if (ret < 0) {
  3671. btrfs_free_path(path);
  3672. return ret;
  3673. }
  3674. WARN_ON(!ret);
  3675. path->slots[0]--;
  3676. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3677. struct btrfs_file_extent_item);
  3678. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3679. found_type = btrfs_key_type(&found_key);
  3680. /* No extents, but there might be delalloc bits */
  3681. if (found_key.objectid != btrfs_ino(inode) ||
  3682. found_type != BTRFS_EXTENT_DATA_KEY) {
  3683. /* have to trust i_size as the end */
  3684. last = (u64)-1;
  3685. last_for_get_extent = isize;
  3686. } else {
  3687. /*
  3688. * remember the start of the last extent. There are a
  3689. * bunch of different factors that go into the length of the
  3690. * extent, so its much less complex to remember where it started
  3691. */
  3692. last = found_key.offset;
  3693. last_for_get_extent = last + 1;
  3694. }
  3695. btrfs_release_path(path);
  3696. /*
  3697. * we might have some extents allocated but more delalloc past those
  3698. * extents. so, we trust isize unless the start of the last extent is
  3699. * beyond isize
  3700. */
  3701. if (last < isize) {
  3702. last = (u64)-1;
  3703. last_for_get_extent = isize;
  3704. }
  3705. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
  3706. &cached_state);
  3707. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3708. get_extent);
  3709. if (!em)
  3710. goto out;
  3711. if (IS_ERR(em)) {
  3712. ret = PTR_ERR(em);
  3713. goto out;
  3714. }
  3715. while (!end) {
  3716. u64 offset_in_extent = 0;
  3717. /* break if the extent we found is outside the range */
  3718. if (em->start >= max || extent_map_end(em) < off)
  3719. break;
  3720. /*
  3721. * get_extent may return an extent that starts before our
  3722. * requested range. We have to make sure the ranges
  3723. * we return to fiemap always move forward and don't
  3724. * overlap, so adjust the offsets here
  3725. */
  3726. em_start = max(em->start, off);
  3727. /*
  3728. * record the offset from the start of the extent
  3729. * for adjusting the disk offset below. Only do this if the
  3730. * extent isn't compressed since our in ram offset may be past
  3731. * what we have actually allocated on disk.
  3732. */
  3733. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3734. offset_in_extent = em_start - em->start;
  3735. em_end = extent_map_end(em);
  3736. em_len = em_end - em_start;
  3737. emflags = em->flags;
  3738. disko = 0;
  3739. flags = 0;
  3740. /*
  3741. * bump off for our next call to get_extent
  3742. */
  3743. off = extent_map_end(em);
  3744. if (off >= max)
  3745. end = 1;
  3746. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3747. end = 1;
  3748. flags |= FIEMAP_EXTENT_LAST;
  3749. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3750. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3751. FIEMAP_EXTENT_NOT_ALIGNED);
  3752. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3753. flags |= (FIEMAP_EXTENT_DELALLOC |
  3754. FIEMAP_EXTENT_UNKNOWN);
  3755. } else {
  3756. unsigned long ref_cnt = 0;
  3757. disko = em->block_start + offset_in_extent;
  3758. /*
  3759. * As btrfs supports shared space, this information
  3760. * can be exported to userspace tools via
  3761. * flag FIEMAP_EXTENT_SHARED.
  3762. */
  3763. ret = iterate_inodes_from_logical(
  3764. em->block_start,
  3765. BTRFS_I(inode)->root->fs_info,
  3766. path, count_ext_ref, &ref_cnt);
  3767. if (ret < 0 && ret != -ENOENT)
  3768. goto out_free;
  3769. if (ref_cnt > 1)
  3770. flags |= FIEMAP_EXTENT_SHARED;
  3771. }
  3772. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3773. flags |= FIEMAP_EXTENT_ENCODED;
  3774. free_extent_map(em);
  3775. em = NULL;
  3776. if ((em_start >= last) || em_len == (u64)-1 ||
  3777. (last == (u64)-1 && isize <= em_end)) {
  3778. flags |= FIEMAP_EXTENT_LAST;
  3779. end = 1;
  3780. }
  3781. /* now scan forward to see if this is really the last extent. */
  3782. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  3783. get_extent);
  3784. if (IS_ERR(em)) {
  3785. ret = PTR_ERR(em);
  3786. goto out;
  3787. }
  3788. if (!em) {
  3789. flags |= FIEMAP_EXTENT_LAST;
  3790. end = 1;
  3791. }
  3792. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  3793. em_len, flags);
  3794. if (ret)
  3795. goto out_free;
  3796. }
  3797. out_free:
  3798. free_extent_map(em);
  3799. out:
  3800. btrfs_free_path(path);
  3801. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  3802. &cached_state, GFP_NOFS);
  3803. return ret;
  3804. }
  3805. static void __free_extent_buffer(struct extent_buffer *eb)
  3806. {
  3807. btrfs_leak_debug_del(&eb->leak_list);
  3808. kmem_cache_free(extent_buffer_cache, eb);
  3809. }
  3810. static int extent_buffer_under_io(struct extent_buffer *eb)
  3811. {
  3812. return (atomic_read(&eb->io_pages) ||
  3813. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  3814. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  3815. }
  3816. /*
  3817. * Helper for releasing extent buffer page.
  3818. */
  3819. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
  3820. unsigned long start_idx)
  3821. {
  3822. unsigned long index;
  3823. unsigned long num_pages;
  3824. struct page *page;
  3825. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  3826. BUG_ON(extent_buffer_under_io(eb));
  3827. num_pages = num_extent_pages(eb->start, eb->len);
  3828. index = start_idx + num_pages;
  3829. if (start_idx >= index)
  3830. return;
  3831. do {
  3832. index--;
  3833. page = extent_buffer_page(eb, index);
  3834. if (page && mapped) {
  3835. spin_lock(&page->mapping->private_lock);
  3836. /*
  3837. * We do this since we'll remove the pages after we've
  3838. * removed the eb from the radix tree, so we could race
  3839. * and have this page now attached to the new eb. So
  3840. * only clear page_private if it's still connected to
  3841. * this eb.
  3842. */
  3843. if (PagePrivate(page) &&
  3844. page->private == (unsigned long)eb) {
  3845. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  3846. BUG_ON(PageDirty(page));
  3847. BUG_ON(PageWriteback(page));
  3848. /*
  3849. * We need to make sure we haven't be attached
  3850. * to a new eb.
  3851. */
  3852. ClearPagePrivate(page);
  3853. set_page_private(page, 0);
  3854. /* One for the page private */
  3855. page_cache_release(page);
  3856. }
  3857. spin_unlock(&page->mapping->private_lock);
  3858. }
  3859. if (page) {
  3860. /* One for when we alloced the page */
  3861. page_cache_release(page);
  3862. }
  3863. } while (index != start_idx);
  3864. }
  3865. /*
  3866. * Helper for releasing the extent buffer.
  3867. */
  3868. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  3869. {
  3870. btrfs_release_extent_buffer_page(eb, 0);
  3871. __free_extent_buffer(eb);
  3872. }
  3873. static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
  3874. u64 start,
  3875. unsigned long len,
  3876. gfp_t mask)
  3877. {
  3878. struct extent_buffer *eb = NULL;
  3879. eb = kmem_cache_zalloc(extent_buffer_cache, mask);
  3880. if (eb == NULL)
  3881. return NULL;
  3882. eb->start = start;
  3883. eb->len = len;
  3884. eb->tree = tree;
  3885. eb->bflags = 0;
  3886. rwlock_init(&eb->lock);
  3887. atomic_set(&eb->write_locks, 0);
  3888. atomic_set(&eb->read_locks, 0);
  3889. atomic_set(&eb->blocking_readers, 0);
  3890. atomic_set(&eb->blocking_writers, 0);
  3891. atomic_set(&eb->spinning_readers, 0);
  3892. atomic_set(&eb->spinning_writers, 0);
  3893. eb->lock_nested = 0;
  3894. init_waitqueue_head(&eb->write_lock_wq);
  3895. init_waitqueue_head(&eb->read_lock_wq);
  3896. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  3897. spin_lock_init(&eb->refs_lock);
  3898. atomic_set(&eb->refs, 1);
  3899. atomic_set(&eb->io_pages, 0);
  3900. /*
  3901. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  3902. */
  3903. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  3904. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  3905. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  3906. return eb;
  3907. }
  3908. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  3909. {
  3910. unsigned long i;
  3911. struct page *p;
  3912. struct extent_buffer *new;
  3913. unsigned long num_pages = num_extent_pages(src->start, src->len);
  3914. new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_NOFS);
  3915. if (new == NULL)
  3916. return NULL;
  3917. for (i = 0; i < num_pages; i++) {
  3918. p = alloc_page(GFP_NOFS);
  3919. if (!p) {
  3920. btrfs_release_extent_buffer(new);
  3921. return NULL;
  3922. }
  3923. attach_extent_buffer_page(new, p);
  3924. WARN_ON(PageDirty(p));
  3925. SetPageUptodate(p);
  3926. new->pages[i] = p;
  3927. }
  3928. copy_extent_buffer(new, src, 0, 0, src->len);
  3929. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  3930. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  3931. return new;
  3932. }
  3933. struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
  3934. {
  3935. struct extent_buffer *eb;
  3936. unsigned long num_pages = num_extent_pages(0, len);
  3937. unsigned long i;
  3938. eb = __alloc_extent_buffer(NULL, start, len, GFP_NOFS);
  3939. if (!eb)
  3940. return NULL;
  3941. for (i = 0; i < num_pages; i++) {
  3942. eb->pages[i] = alloc_page(GFP_NOFS);
  3943. if (!eb->pages[i])
  3944. goto err;
  3945. }
  3946. set_extent_buffer_uptodate(eb);
  3947. btrfs_set_header_nritems(eb, 0);
  3948. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  3949. return eb;
  3950. err:
  3951. for (; i > 0; i--)
  3952. __free_page(eb->pages[i - 1]);
  3953. __free_extent_buffer(eb);
  3954. return NULL;
  3955. }
  3956. static void check_buffer_tree_ref(struct extent_buffer *eb)
  3957. {
  3958. int refs;
  3959. /* the ref bit is tricky. We have to make sure it is set
  3960. * if we have the buffer dirty. Otherwise the
  3961. * code to free a buffer can end up dropping a dirty
  3962. * page
  3963. *
  3964. * Once the ref bit is set, it won't go away while the
  3965. * buffer is dirty or in writeback, and it also won't
  3966. * go away while we have the reference count on the
  3967. * eb bumped.
  3968. *
  3969. * We can't just set the ref bit without bumping the
  3970. * ref on the eb because free_extent_buffer might
  3971. * see the ref bit and try to clear it. If this happens
  3972. * free_extent_buffer might end up dropping our original
  3973. * ref by mistake and freeing the page before we are able
  3974. * to add one more ref.
  3975. *
  3976. * So bump the ref count first, then set the bit. If someone
  3977. * beat us to it, drop the ref we added.
  3978. */
  3979. refs = atomic_read(&eb->refs);
  3980. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3981. return;
  3982. spin_lock(&eb->refs_lock);
  3983. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  3984. atomic_inc(&eb->refs);
  3985. spin_unlock(&eb->refs_lock);
  3986. }
  3987. static void mark_extent_buffer_accessed(struct extent_buffer *eb)
  3988. {
  3989. unsigned long num_pages, i;
  3990. check_buffer_tree_ref(eb);
  3991. num_pages = num_extent_pages(eb->start, eb->len);
  3992. for (i = 0; i < num_pages; i++) {
  3993. struct page *p = extent_buffer_page(eb, i);
  3994. mark_page_accessed(p);
  3995. }
  3996. }
  3997. struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
  3998. u64 start)
  3999. {
  4000. struct extent_buffer *eb;
  4001. rcu_read_lock();
  4002. eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
  4003. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4004. rcu_read_unlock();
  4005. mark_extent_buffer_accessed(eb);
  4006. return eb;
  4007. }
  4008. rcu_read_unlock();
  4009. return NULL;
  4010. }
  4011. struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
  4012. u64 start, unsigned long len)
  4013. {
  4014. unsigned long num_pages = num_extent_pages(start, len);
  4015. unsigned long i;
  4016. unsigned long index = start >> PAGE_CACHE_SHIFT;
  4017. struct extent_buffer *eb;
  4018. struct extent_buffer *exists = NULL;
  4019. struct page *p;
  4020. struct address_space *mapping = tree->mapping;
  4021. int uptodate = 1;
  4022. int ret;
  4023. eb = find_extent_buffer(tree, start);
  4024. if (eb)
  4025. return eb;
  4026. eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
  4027. if (!eb)
  4028. return NULL;
  4029. for (i = 0; i < num_pages; i++, index++) {
  4030. p = find_or_create_page(mapping, index, GFP_NOFS);
  4031. if (!p)
  4032. goto free_eb;
  4033. spin_lock(&mapping->private_lock);
  4034. if (PagePrivate(p)) {
  4035. /*
  4036. * We could have already allocated an eb for this page
  4037. * and attached one so lets see if we can get a ref on
  4038. * the existing eb, and if we can we know it's good and
  4039. * we can just return that one, else we know we can just
  4040. * overwrite page->private.
  4041. */
  4042. exists = (struct extent_buffer *)p->private;
  4043. if (atomic_inc_not_zero(&exists->refs)) {
  4044. spin_unlock(&mapping->private_lock);
  4045. unlock_page(p);
  4046. page_cache_release(p);
  4047. mark_extent_buffer_accessed(exists);
  4048. goto free_eb;
  4049. }
  4050. /*
  4051. * Do this so attach doesn't complain and we need to
  4052. * drop the ref the old guy had.
  4053. */
  4054. ClearPagePrivate(p);
  4055. WARN_ON(PageDirty(p));
  4056. page_cache_release(p);
  4057. }
  4058. attach_extent_buffer_page(eb, p);
  4059. spin_unlock(&mapping->private_lock);
  4060. WARN_ON(PageDirty(p));
  4061. mark_page_accessed(p);
  4062. eb->pages[i] = p;
  4063. if (!PageUptodate(p))
  4064. uptodate = 0;
  4065. /*
  4066. * see below about how we avoid a nasty race with release page
  4067. * and why we unlock later
  4068. */
  4069. }
  4070. if (uptodate)
  4071. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4072. again:
  4073. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  4074. if (ret)
  4075. goto free_eb;
  4076. spin_lock(&tree->buffer_lock);
  4077. ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
  4078. spin_unlock(&tree->buffer_lock);
  4079. radix_tree_preload_end();
  4080. if (ret == -EEXIST) {
  4081. exists = find_extent_buffer(tree, start);
  4082. if (exists)
  4083. goto free_eb;
  4084. else
  4085. goto again;
  4086. }
  4087. /* add one reference for the tree */
  4088. check_buffer_tree_ref(eb);
  4089. /*
  4090. * there is a race where release page may have
  4091. * tried to find this extent buffer in the radix
  4092. * but failed. It will tell the VM it is safe to
  4093. * reclaim the, and it will clear the page private bit.
  4094. * We must make sure to set the page private bit properly
  4095. * after the extent buffer is in the radix tree so
  4096. * it doesn't get lost
  4097. */
  4098. SetPageChecked(eb->pages[0]);
  4099. for (i = 1; i < num_pages; i++) {
  4100. p = extent_buffer_page(eb, i);
  4101. ClearPageChecked(p);
  4102. unlock_page(p);
  4103. }
  4104. unlock_page(eb->pages[0]);
  4105. return eb;
  4106. free_eb:
  4107. for (i = 0; i < num_pages; i++) {
  4108. if (eb->pages[i])
  4109. unlock_page(eb->pages[i]);
  4110. }
  4111. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4112. btrfs_release_extent_buffer(eb);
  4113. return exists;
  4114. }
  4115. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4116. {
  4117. struct extent_buffer *eb =
  4118. container_of(head, struct extent_buffer, rcu_head);
  4119. __free_extent_buffer(eb);
  4120. }
  4121. /* Expects to have eb->eb_lock already held */
  4122. static int release_extent_buffer(struct extent_buffer *eb)
  4123. {
  4124. WARN_ON(atomic_read(&eb->refs) == 0);
  4125. if (atomic_dec_and_test(&eb->refs)) {
  4126. if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
  4127. spin_unlock(&eb->refs_lock);
  4128. } else {
  4129. struct extent_io_tree *tree = eb->tree;
  4130. spin_unlock(&eb->refs_lock);
  4131. spin_lock(&tree->buffer_lock);
  4132. radix_tree_delete(&tree->buffer,
  4133. eb->start >> PAGE_CACHE_SHIFT);
  4134. spin_unlock(&tree->buffer_lock);
  4135. }
  4136. /* Should be safe to release our pages at this point */
  4137. btrfs_release_extent_buffer_page(eb, 0);
  4138. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4139. return 1;
  4140. }
  4141. spin_unlock(&eb->refs_lock);
  4142. return 0;
  4143. }
  4144. void free_extent_buffer(struct extent_buffer *eb)
  4145. {
  4146. int refs;
  4147. int old;
  4148. if (!eb)
  4149. return;
  4150. while (1) {
  4151. refs = atomic_read(&eb->refs);
  4152. if (refs <= 3)
  4153. break;
  4154. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4155. if (old == refs)
  4156. return;
  4157. }
  4158. spin_lock(&eb->refs_lock);
  4159. if (atomic_read(&eb->refs) == 2 &&
  4160. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4161. atomic_dec(&eb->refs);
  4162. if (atomic_read(&eb->refs) == 2 &&
  4163. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4164. !extent_buffer_under_io(eb) &&
  4165. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4166. atomic_dec(&eb->refs);
  4167. /*
  4168. * I know this is terrible, but it's temporary until we stop tracking
  4169. * the uptodate bits and such for the extent buffers.
  4170. */
  4171. release_extent_buffer(eb);
  4172. }
  4173. void free_extent_buffer_stale(struct extent_buffer *eb)
  4174. {
  4175. if (!eb)
  4176. return;
  4177. spin_lock(&eb->refs_lock);
  4178. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4179. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4180. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4181. atomic_dec(&eb->refs);
  4182. release_extent_buffer(eb);
  4183. }
  4184. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4185. {
  4186. unsigned long i;
  4187. unsigned long num_pages;
  4188. struct page *page;
  4189. num_pages = num_extent_pages(eb->start, eb->len);
  4190. for (i = 0; i < num_pages; i++) {
  4191. page = extent_buffer_page(eb, i);
  4192. if (!PageDirty(page))
  4193. continue;
  4194. lock_page(page);
  4195. WARN_ON(!PagePrivate(page));
  4196. clear_page_dirty_for_io(page);
  4197. spin_lock_irq(&page->mapping->tree_lock);
  4198. if (!PageDirty(page)) {
  4199. radix_tree_tag_clear(&page->mapping->page_tree,
  4200. page_index(page),
  4201. PAGECACHE_TAG_DIRTY);
  4202. }
  4203. spin_unlock_irq(&page->mapping->tree_lock);
  4204. ClearPageError(page);
  4205. unlock_page(page);
  4206. }
  4207. WARN_ON(atomic_read(&eb->refs) == 0);
  4208. }
  4209. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4210. {
  4211. unsigned long i;
  4212. unsigned long num_pages;
  4213. int was_dirty = 0;
  4214. check_buffer_tree_ref(eb);
  4215. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4216. num_pages = num_extent_pages(eb->start, eb->len);
  4217. WARN_ON(atomic_read(&eb->refs) == 0);
  4218. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4219. for (i = 0; i < num_pages; i++)
  4220. set_page_dirty(extent_buffer_page(eb, i));
  4221. return was_dirty;
  4222. }
  4223. int clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4224. {
  4225. unsigned long i;
  4226. struct page *page;
  4227. unsigned long num_pages;
  4228. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4229. num_pages = num_extent_pages(eb->start, eb->len);
  4230. for (i = 0; i < num_pages; i++) {
  4231. page = extent_buffer_page(eb, i);
  4232. if (page)
  4233. ClearPageUptodate(page);
  4234. }
  4235. return 0;
  4236. }
  4237. int set_extent_buffer_uptodate(struct extent_buffer *eb)
  4238. {
  4239. unsigned long i;
  4240. struct page *page;
  4241. unsigned long num_pages;
  4242. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4243. num_pages = num_extent_pages(eb->start, eb->len);
  4244. for (i = 0; i < num_pages; i++) {
  4245. page = extent_buffer_page(eb, i);
  4246. SetPageUptodate(page);
  4247. }
  4248. return 0;
  4249. }
  4250. int extent_buffer_uptodate(struct extent_buffer *eb)
  4251. {
  4252. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4253. }
  4254. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4255. struct extent_buffer *eb, u64 start, int wait,
  4256. get_extent_t *get_extent, int mirror_num)
  4257. {
  4258. unsigned long i;
  4259. unsigned long start_i;
  4260. struct page *page;
  4261. int err;
  4262. int ret = 0;
  4263. int locked_pages = 0;
  4264. int all_uptodate = 1;
  4265. unsigned long num_pages;
  4266. unsigned long num_reads = 0;
  4267. struct bio *bio = NULL;
  4268. unsigned long bio_flags = 0;
  4269. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4270. return 0;
  4271. if (start) {
  4272. WARN_ON(start < eb->start);
  4273. start_i = (start >> PAGE_CACHE_SHIFT) -
  4274. (eb->start >> PAGE_CACHE_SHIFT);
  4275. } else {
  4276. start_i = 0;
  4277. }
  4278. num_pages = num_extent_pages(eb->start, eb->len);
  4279. for (i = start_i; i < num_pages; i++) {
  4280. page = extent_buffer_page(eb, i);
  4281. if (wait == WAIT_NONE) {
  4282. if (!trylock_page(page))
  4283. goto unlock_exit;
  4284. } else {
  4285. lock_page(page);
  4286. }
  4287. locked_pages++;
  4288. if (!PageUptodate(page)) {
  4289. num_reads++;
  4290. all_uptodate = 0;
  4291. }
  4292. }
  4293. if (all_uptodate) {
  4294. if (start_i == 0)
  4295. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4296. goto unlock_exit;
  4297. }
  4298. clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
  4299. eb->read_mirror = 0;
  4300. atomic_set(&eb->io_pages, num_reads);
  4301. for (i = start_i; i < num_pages; i++) {
  4302. page = extent_buffer_page(eb, i);
  4303. if (!PageUptodate(page)) {
  4304. ClearPageError(page);
  4305. err = __extent_read_full_page(tree, page,
  4306. get_extent, &bio,
  4307. mirror_num, &bio_flags,
  4308. READ | REQ_META);
  4309. if (err)
  4310. ret = err;
  4311. } else {
  4312. unlock_page(page);
  4313. }
  4314. }
  4315. if (bio) {
  4316. err = submit_one_bio(READ | REQ_META, bio, mirror_num,
  4317. bio_flags);
  4318. if (err)
  4319. return err;
  4320. }
  4321. if (ret || wait != WAIT_COMPLETE)
  4322. return ret;
  4323. for (i = start_i; i < num_pages; i++) {
  4324. page = extent_buffer_page(eb, i);
  4325. wait_on_page_locked(page);
  4326. if (!PageUptodate(page))
  4327. ret = -EIO;
  4328. }
  4329. return ret;
  4330. unlock_exit:
  4331. i = start_i;
  4332. while (locked_pages > 0) {
  4333. page = extent_buffer_page(eb, i);
  4334. i++;
  4335. unlock_page(page);
  4336. locked_pages--;
  4337. }
  4338. return ret;
  4339. }
  4340. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4341. unsigned long start,
  4342. unsigned long len)
  4343. {
  4344. size_t cur;
  4345. size_t offset;
  4346. struct page *page;
  4347. char *kaddr;
  4348. char *dst = (char *)dstv;
  4349. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4350. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4351. WARN_ON(start > eb->len);
  4352. WARN_ON(start + len > eb->start + eb->len);
  4353. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4354. while (len > 0) {
  4355. page = extent_buffer_page(eb, i);
  4356. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4357. kaddr = page_address(page);
  4358. memcpy(dst, kaddr + offset, cur);
  4359. dst += cur;
  4360. len -= cur;
  4361. offset = 0;
  4362. i++;
  4363. }
  4364. }
  4365. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4366. unsigned long min_len, char **map,
  4367. unsigned long *map_start,
  4368. unsigned long *map_len)
  4369. {
  4370. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  4371. char *kaddr;
  4372. struct page *p;
  4373. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4374. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4375. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4376. PAGE_CACHE_SHIFT;
  4377. if (i != end_i)
  4378. return -EINVAL;
  4379. if (i == 0) {
  4380. offset = start_offset;
  4381. *map_start = 0;
  4382. } else {
  4383. offset = 0;
  4384. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  4385. }
  4386. if (start + min_len > eb->len) {
  4387. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  4388. "wanted %lu %lu\n",
  4389. eb->start, eb->len, start, min_len);
  4390. return -EINVAL;
  4391. }
  4392. p = extent_buffer_page(eb, i);
  4393. kaddr = page_address(p);
  4394. *map = kaddr + offset;
  4395. *map_len = PAGE_CACHE_SIZE - offset;
  4396. return 0;
  4397. }
  4398. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4399. unsigned long start,
  4400. unsigned long len)
  4401. {
  4402. size_t cur;
  4403. size_t offset;
  4404. struct page *page;
  4405. char *kaddr;
  4406. char *ptr = (char *)ptrv;
  4407. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4408. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4409. int ret = 0;
  4410. WARN_ON(start > eb->len);
  4411. WARN_ON(start + len > eb->start + eb->len);
  4412. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4413. while (len > 0) {
  4414. page = extent_buffer_page(eb, i);
  4415. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4416. kaddr = page_address(page);
  4417. ret = memcmp(ptr, kaddr + offset, cur);
  4418. if (ret)
  4419. break;
  4420. ptr += cur;
  4421. len -= cur;
  4422. offset = 0;
  4423. i++;
  4424. }
  4425. return ret;
  4426. }
  4427. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4428. unsigned long start, unsigned long len)
  4429. {
  4430. size_t cur;
  4431. size_t offset;
  4432. struct page *page;
  4433. char *kaddr;
  4434. char *src = (char *)srcv;
  4435. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4436. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4437. WARN_ON(start > eb->len);
  4438. WARN_ON(start + len > eb->start + eb->len);
  4439. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4440. while (len > 0) {
  4441. page = extent_buffer_page(eb, i);
  4442. WARN_ON(!PageUptodate(page));
  4443. cur = min(len, PAGE_CACHE_SIZE - offset);
  4444. kaddr = page_address(page);
  4445. memcpy(kaddr + offset, src, cur);
  4446. src += cur;
  4447. len -= cur;
  4448. offset = 0;
  4449. i++;
  4450. }
  4451. }
  4452. void memset_extent_buffer(struct extent_buffer *eb, char c,
  4453. unsigned long start, unsigned long len)
  4454. {
  4455. size_t cur;
  4456. size_t offset;
  4457. struct page *page;
  4458. char *kaddr;
  4459. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4460. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4461. WARN_ON(start > eb->len);
  4462. WARN_ON(start + len > eb->start + eb->len);
  4463. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4464. while (len > 0) {
  4465. page = extent_buffer_page(eb, i);
  4466. WARN_ON(!PageUptodate(page));
  4467. cur = min(len, PAGE_CACHE_SIZE - offset);
  4468. kaddr = page_address(page);
  4469. memset(kaddr + offset, c, cur);
  4470. len -= cur;
  4471. offset = 0;
  4472. i++;
  4473. }
  4474. }
  4475. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4476. unsigned long dst_offset, unsigned long src_offset,
  4477. unsigned long len)
  4478. {
  4479. u64 dst_len = dst->len;
  4480. size_t cur;
  4481. size_t offset;
  4482. struct page *page;
  4483. char *kaddr;
  4484. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4485. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4486. WARN_ON(src->len != dst_len);
  4487. offset = (start_offset + dst_offset) &
  4488. (PAGE_CACHE_SIZE - 1);
  4489. while (len > 0) {
  4490. page = extent_buffer_page(dst, i);
  4491. WARN_ON(!PageUptodate(page));
  4492. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  4493. kaddr = page_address(page);
  4494. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4495. src_offset += cur;
  4496. len -= cur;
  4497. offset = 0;
  4498. i++;
  4499. }
  4500. }
  4501. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  4502. {
  4503. unsigned long distance = (src > dst) ? src - dst : dst - src;
  4504. return distance < len;
  4505. }
  4506. static void copy_pages(struct page *dst_page, struct page *src_page,
  4507. unsigned long dst_off, unsigned long src_off,
  4508. unsigned long len)
  4509. {
  4510. char *dst_kaddr = page_address(dst_page);
  4511. char *src_kaddr;
  4512. int must_memmove = 0;
  4513. if (dst_page != src_page) {
  4514. src_kaddr = page_address(src_page);
  4515. } else {
  4516. src_kaddr = dst_kaddr;
  4517. if (areas_overlap(src_off, dst_off, len))
  4518. must_memmove = 1;
  4519. }
  4520. if (must_memmove)
  4521. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4522. else
  4523. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4524. }
  4525. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4526. unsigned long src_offset, unsigned long len)
  4527. {
  4528. size_t cur;
  4529. size_t dst_off_in_page;
  4530. size_t src_off_in_page;
  4531. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4532. unsigned long dst_i;
  4533. unsigned long src_i;
  4534. if (src_offset + len > dst->len) {
  4535. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  4536. "len %lu dst len %lu\n", src_offset, len, dst->len);
  4537. BUG_ON(1);
  4538. }
  4539. if (dst_offset + len > dst->len) {
  4540. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  4541. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  4542. BUG_ON(1);
  4543. }
  4544. while (len > 0) {
  4545. dst_off_in_page = (start_offset + dst_offset) &
  4546. (PAGE_CACHE_SIZE - 1);
  4547. src_off_in_page = (start_offset + src_offset) &
  4548. (PAGE_CACHE_SIZE - 1);
  4549. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4550. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  4551. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  4552. src_off_in_page));
  4553. cur = min_t(unsigned long, cur,
  4554. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  4555. copy_pages(extent_buffer_page(dst, dst_i),
  4556. extent_buffer_page(dst, src_i),
  4557. dst_off_in_page, src_off_in_page, cur);
  4558. src_offset += cur;
  4559. dst_offset += cur;
  4560. len -= cur;
  4561. }
  4562. }
  4563. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4564. unsigned long src_offset, unsigned long len)
  4565. {
  4566. size_t cur;
  4567. size_t dst_off_in_page;
  4568. size_t src_off_in_page;
  4569. unsigned long dst_end = dst_offset + len - 1;
  4570. unsigned long src_end = src_offset + len - 1;
  4571. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4572. unsigned long dst_i;
  4573. unsigned long src_i;
  4574. if (src_offset + len > dst->len) {
  4575. printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
  4576. "len %lu len %lu\n", src_offset, len, dst->len);
  4577. BUG_ON(1);
  4578. }
  4579. if (dst_offset + len > dst->len) {
  4580. printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
  4581. "len %lu len %lu\n", dst_offset, len, dst->len);
  4582. BUG_ON(1);
  4583. }
  4584. if (dst_offset < src_offset) {
  4585. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  4586. return;
  4587. }
  4588. while (len > 0) {
  4589. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  4590. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  4591. dst_off_in_page = (start_offset + dst_end) &
  4592. (PAGE_CACHE_SIZE - 1);
  4593. src_off_in_page = (start_offset + src_end) &
  4594. (PAGE_CACHE_SIZE - 1);
  4595. cur = min_t(unsigned long, len, src_off_in_page + 1);
  4596. cur = min(cur, dst_off_in_page + 1);
  4597. copy_pages(extent_buffer_page(dst, dst_i),
  4598. extent_buffer_page(dst, src_i),
  4599. dst_off_in_page - cur + 1,
  4600. src_off_in_page - cur + 1, cur);
  4601. dst_end -= cur;
  4602. src_end -= cur;
  4603. len -= cur;
  4604. }
  4605. }
  4606. int try_release_extent_buffer(struct page *page)
  4607. {
  4608. struct extent_buffer *eb;
  4609. /*
  4610. * We need to make sure noboody is attaching this page to an eb right
  4611. * now.
  4612. */
  4613. spin_lock(&page->mapping->private_lock);
  4614. if (!PagePrivate(page)) {
  4615. spin_unlock(&page->mapping->private_lock);
  4616. return 1;
  4617. }
  4618. eb = (struct extent_buffer *)page->private;
  4619. BUG_ON(!eb);
  4620. /*
  4621. * This is a little awful but should be ok, we need to make sure that
  4622. * the eb doesn't disappear out from under us while we're looking at
  4623. * this page.
  4624. */
  4625. spin_lock(&eb->refs_lock);
  4626. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  4627. spin_unlock(&eb->refs_lock);
  4628. spin_unlock(&page->mapping->private_lock);
  4629. return 0;
  4630. }
  4631. spin_unlock(&page->mapping->private_lock);
  4632. /*
  4633. * If tree ref isn't set then we know the ref on this eb is a real ref,
  4634. * so just return, this page will likely be freed soon anyway.
  4635. */
  4636. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  4637. spin_unlock(&eb->refs_lock);
  4638. return 0;
  4639. }
  4640. return release_extent_buffer(eb);
  4641. }