extent_io.c 148 KB

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