extent_io.c 131 KB

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