extent_io.c 131 KB

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