extent_io.c 131 KB

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