extent_io.c 131 KB

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