extent_io.c 132 KB

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