extent_io.c 149 KB

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