extent_io.c 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929
  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. refcount_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. refcount_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 (refcount_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. refcount_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. refcount_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. refcount_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. refcount_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. err = -EAGAIN;
  1533. goto out;
  1534. }
  1535. for (i = 0; i < ret; i++) {
  1536. if (page_ops & PAGE_SET_PRIVATE2)
  1537. SetPagePrivate2(pages[i]);
  1538. if (pages[i] == locked_page) {
  1539. put_page(pages[i]);
  1540. pages_locked++;
  1541. continue;
  1542. }
  1543. if (page_ops & PAGE_CLEAR_DIRTY)
  1544. clear_page_dirty_for_io(pages[i]);
  1545. if (page_ops & PAGE_SET_WRITEBACK)
  1546. set_page_writeback(pages[i]);
  1547. if (page_ops & PAGE_SET_ERROR)
  1548. SetPageError(pages[i]);
  1549. if (page_ops & PAGE_END_WRITEBACK)
  1550. end_page_writeback(pages[i]);
  1551. if (page_ops & PAGE_UNLOCK)
  1552. unlock_page(pages[i]);
  1553. if (page_ops & PAGE_LOCK) {
  1554. lock_page(pages[i]);
  1555. if (!PageDirty(pages[i]) ||
  1556. pages[i]->mapping != mapping) {
  1557. unlock_page(pages[i]);
  1558. put_page(pages[i]);
  1559. err = -EAGAIN;
  1560. goto out;
  1561. }
  1562. }
  1563. put_page(pages[i]);
  1564. pages_locked++;
  1565. }
  1566. nr_pages -= ret;
  1567. index += ret;
  1568. cond_resched();
  1569. }
  1570. out:
  1571. if (err && index_ret)
  1572. *index_ret = start_index + pages_locked - 1;
  1573. return err;
  1574. }
  1575. void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
  1576. u64 delalloc_end, struct page *locked_page,
  1577. unsigned clear_bits,
  1578. unsigned long page_ops)
  1579. {
  1580. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits, 1, 0,
  1581. NULL, GFP_NOFS);
  1582. __process_pages_contig(inode->i_mapping, locked_page,
  1583. start >> PAGE_SHIFT, end >> PAGE_SHIFT,
  1584. page_ops, NULL);
  1585. }
  1586. /*
  1587. * count the number of bytes in the tree that have a given bit(s)
  1588. * set. This can be fairly slow, except for EXTENT_DIRTY which is
  1589. * cached. The total number found is returned.
  1590. */
  1591. u64 count_range_bits(struct extent_io_tree *tree,
  1592. u64 *start, u64 search_end, u64 max_bytes,
  1593. unsigned bits, int contig)
  1594. {
  1595. struct rb_node *node;
  1596. struct extent_state *state;
  1597. u64 cur_start = *start;
  1598. u64 total_bytes = 0;
  1599. u64 last = 0;
  1600. int found = 0;
  1601. if (WARN_ON(search_end <= cur_start))
  1602. return 0;
  1603. spin_lock(&tree->lock);
  1604. if (cur_start == 0 && bits == EXTENT_DIRTY) {
  1605. total_bytes = tree->dirty_bytes;
  1606. goto out;
  1607. }
  1608. /*
  1609. * this search will find all the extents that end after
  1610. * our range starts.
  1611. */
  1612. node = tree_search(tree, cur_start);
  1613. if (!node)
  1614. goto out;
  1615. while (1) {
  1616. state = rb_entry(node, struct extent_state, rb_node);
  1617. if (state->start > search_end)
  1618. break;
  1619. if (contig && found && state->start > last + 1)
  1620. break;
  1621. if (state->end >= cur_start && (state->state & bits) == bits) {
  1622. total_bytes += min(search_end, state->end) + 1 -
  1623. max(cur_start, state->start);
  1624. if (total_bytes >= max_bytes)
  1625. break;
  1626. if (!found) {
  1627. *start = max(cur_start, state->start);
  1628. found = 1;
  1629. }
  1630. last = state->end;
  1631. } else if (contig && found) {
  1632. break;
  1633. }
  1634. node = rb_next(node);
  1635. if (!node)
  1636. break;
  1637. }
  1638. out:
  1639. spin_unlock(&tree->lock);
  1640. return total_bytes;
  1641. }
  1642. /*
  1643. * set the private field for a given byte offset in the tree. If there isn't
  1644. * an extent_state there already, this does nothing.
  1645. */
  1646. static noinline int set_state_failrec(struct extent_io_tree *tree, u64 start,
  1647. struct io_failure_record *failrec)
  1648. {
  1649. struct rb_node *node;
  1650. struct extent_state *state;
  1651. int ret = 0;
  1652. spin_lock(&tree->lock);
  1653. /*
  1654. * this search will find all the extents that end after
  1655. * our range starts.
  1656. */
  1657. node = tree_search(tree, start);
  1658. if (!node) {
  1659. ret = -ENOENT;
  1660. goto out;
  1661. }
  1662. state = rb_entry(node, struct extent_state, rb_node);
  1663. if (state->start != start) {
  1664. ret = -ENOENT;
  1665. goto out;
  1666. }
  1667. state->failrec = failrec;
  1668. out:
  1669. spin_unlock(&tree->lock);
  1670. return ret;
  1671. }
  1672. static noinline int get_state_failrec(struct extent_io_tree *tree, u64 start,
  1673. struct io_failure_record **failrec)
  1674. {
  1675. struct rb_node *node;
  1676. struct extent_state *state;
  1677. int ret = 0;
  1678. spin_lock(&tree->lock);
  1679. /*
  1680. * this search will find all the extents that end after
  1681. * our range starts.
  1682. */
  1683. node = tree_search(tree, start);
  1684. if (!node) {
  1685. ret = -ENOENT;
  1686. goto out;
  1687. }
  1688. state = rb_entry(node, struct extent_state, rb_node);
  1689. if (state->start != start) {
  1690. ret = -ENOENT;
  1691. goto out;
  1692. }
  1693. *failrec = state->failrec;
  1694. out:
  1695. spin_unlock(&tree->lock);
  1696. return ret;
  1697. }
  1698. /*
  1699. * searches a range in the state tree for a given mask.
  1700. * If 'filled' == 1, this returns 1 only if every extent in the tree
  1701. * has the bits set. Otherwise, 1 is returned if any bit in the
  1702. * range is found set.
  1703. */
  1704. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  1705. unsigned bits, int filled, struct extent_state *cached)
  1706. {
  1707. struct extent_state *state = NULL;
  1708. struct rb_node *node;
  1709. int bitset = 0;
  1710. spin_lock(&tree->lock);
  1711. if (cached && extent_state_in_tree(cached) && cached->start <= start &&
  1712. cached->end > start)
  1713. node = &cached->rb_node;
  1714. else
  1715. node = tree_search(tree, start);
  1716. while (node && start <= end) {
  1717. state = rb_entry(node, struct extent_state, rb_node);
  1718. if (filled && state->start > start) {
  1719. bitset = 0;
  1720. break;
  1721. }
  1722. if (state->start > end)
  1723. break;
  1724. if (state->state & bits) {
  1725. bitset = 1;
  1726. if (!filled)
  1727. break;
  1728. } else if (filled) {
  1729. bitset = 0;
  1730. break;
  1731. }
  1732. if (state->end == (u64)-1)
  1733. break;
  1734. start = state->end + 1;
  1735. if (start > end)
  1736. break;
  1737. node = rb_next(node);
  1738. if (!node) {
  1739. if (filled)
  1740. bitset = 0;
  1741. break;
  1742. }
  1743. }
  1744. spin_unlock(&tree->lock);
  1745. return bitset;
  1746. }
  1747. /*
  1748. * helper function to set a given page up to date if all the
  1749. * extents in the tree for that page are up to date
  1750. */
  1751. static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
  1752. {
  1753. u64 start = page_offset(page);
  1754. u64 end = start + PAGE_SIZE - 1;
  1755. if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
  1756. SetPageUptodate(page);
  1757. }
  1758. int free_io_failure(struct btrfs_inode *inode, struct io_failure_record *rec)
  1759. {
  1760. int ret;
  1761. int err = 0;
  1762. struct extent_io_tree *failure_tree = &inode->io_failure_tree;
  1763. set_state_failrec(failure_tree, rec->start, NULL);
  1764. ret = clear_extent_bits(failure_tree, rec->start,
  1765. rec->start + rec->len - 1,
  1766. EXTENT_LOCKED | EXTENT_DIRTY);
  1767. if (ret)
  1768. err = ret;
  1769. ret = clear_extent_bits(&inode->io_tree, rec->start,
  1770. rec->start + rec->len - 1,
  1771. EXTENT_DAMAGED);
  1772. if (ret && !err)
  1773. err = ret;
  1774. kfree(rec);
  1775. return err;
  1776. }
  1777. /*
  1778. * this bypasses the standard btrfs submit functions deliberately, as
  1779. * the standard behavior is to write all copies in a raid setup. here we only
  1780. * want to write the one bad copy. so we do the mapping for ourselves and issue
  1781. * submit_bio directly.
  1782. * to avoid any synchronization issues, wait for the data after writing, which
  1783. * actually prevents the read that triggered the error from finishing.
  1784. * currently, there can be no more than two copies of every data bit. thus,
  1785. * exactly one rewrite is required.
  1786. */
  1787. int repair_io_failure(struct btrfs_inode *inode, u64 start, u64 length,
  1788. u64 logical, struct page *page,
  1789. unsigned int pg_offset, int mirror_num)
  1790. {
  1791. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1792. struct bio *bio;
  1793. struct btrfs_device *dev;
  1794. u64 map_length = 0;
  1795. u64 sector;
  1796. struct btrfs_bio *bbio = NULL;
  1797. int ret;
  1798. ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
  1799. BUG_ON(!mirror_num);
  1800. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  1801. if (!bio)
  1802. return -EIO;
  1803. bio->bi_iter.bi_size = 0;
  1804. map_length = length;
  1805. /*
  1806. * Avoid races with device replace and make sure our bbio has devices
  1807. * associated to its stripes that don't go away while we are doing the
  1808. * read repair operation.
  1809. */
  1810. btrfs_bio_counter_inc_blocked(fs_info);
  1811. if (btrfs_is_parity_mirror(fs_info, logical, length, mirror_num)) {
  1812. /*
  1813. * Note that we don't use BTRFS_MAP_WRITE because it's supposed
  1814. * to update all raid stripes, but here we just want to correct
  1815. * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
  1816. * stripe's dev and sector.
  1817. */
  1818. ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
  1819. &map_length, &bbio, 0);
  1820. if (ret) {
  1821. btrfs_bio_counter_dec(fs_info);
  1822. bio_put(bio);
  1823. return -EIO;
  1824. }
  1825. ASSERT(bbio->mirror_num == 1);
  1826. } else {
  1827. ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
  1828. &map_length, &bbio, mirror_num);
  1829. if (ret) {
  1830. btrfs_bio_counter_dec(fs_info);
  1831. bio_put(bio);
  1832. return -EIO;
  1833. }
  1834. BUG_ON(mirror_num != bbio->mirror_num);
  1835. }
  1836. sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
  1837. bio->bi_iter.bi_sector = sector;
  1838. dev = bbio->stripes[bbio->mirror_num - 1].dev;
  1839. btrfs_put_bbio(bbio);
  1840. if (!dev || !dev->bdev || !dev->writeable) {
  1841. btrfs_bio_counter_dec(fs_info);
  1842. bio_put(bio);
  1843. return -EIO;
  1844. }
  1845. bio->bi_bdev = dev->bdev;
  1846. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC;
  1847. bio_add_page(bio, page, length, pg_offset);
  1848. if (btrfsic_submit_bio_wait(bio)) {
  1849. /* try to remap that extent elsewhere? */
  1850. btrfs_bio_counter_dec(fs_info);
  1851. bio_put(bio);
  1852. btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
  1853. return -EIO;
  1854. }
  1855. btrfs_info_rl_in_rcu(fs_info,
  1856. "read error corrected: ino %llu off %llu (dev %s sector %llu)",
  1857. btrfs_ino(inode), start,
  1858. rcu_str_deref(dev->name), sector);
  1859. btrfs_bio_counter_dec(fs_info);
  1860. bio_put(bio);
  1861. return 0;
  1862. }
  1863. int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
  1864. struct extent_buffer *eb, int mirror_num)
  1865. {
  1866. u64 start = eb->start;
  1867. unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
  1868. int ret = 0;
  1869. if (fs_info->sb->s_flags & MS_RDONLY)
  1870. return -EROFS;
  1871. for (i = 0; i < num_pages; i++) {
  1872. struct page *p = eb->pages[i];
  1873. ret = repair_io_failure(BTRFS_I(fs_info->btree_inode), start,
  1874. PAGE_SIZE, start, p,
  1875. start - page_offset(p), mirror_num);
  1876. if (ret)
  1877. break;
  1878. start += PAGE_SIZE;
  1879. }
  1880. return ret;
  1881. }
  1882. /*
  1883. * each time an IO finishes, we do a fast check in the IO failure tree
  1884. * to see if we need to process or clean up an io_failure_record
  1885. */
  1886. int clean_io_failure(struct btrfs_inode *inode, u64 start, struct page *page,
  1887. unsigned int pg_offset)
  1888. {
  1889. u64 private;
  1890. struct io_failure_record *failrec;
  1891. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  1892. struct extent_state *state;
  1893. int num_copies;
  1894. int ret;
  1895. private = 0;
  1896. ret = count_range_bits(&inode->io_failure_tree, &private,
  1897. (u64)-1, 1, EXTENT_DIRTY, 0);
  1898. if (!ret)
  1899. return 0;
  1900. ret = get_state_failrec(&inode->io_failure_tree, start,
  1901. &failrec);
  1902. if (ret)
  1903. return 0;
  1904. BUG_ON(!failrec->this_mirror);
  1905. if (failrec->in_validation) {
  1906. /* there was no real error, just free the record */
  1907. btrfs_debug(fs_info,
  1908. "clean_io_failure: freeing dummy error at %llu",
  1909. failrec->start);
  1910. goto out;
  1911. }
  1912. if (fs_info->sb->s_flags & MS_RDONLY)
  1913. goto out;
  1914. spin_lock(&inode->io_tree.lock);
  1915. state = find_first_extent_bit_state(&inode->io_tree,
  1916. failrec->start,
  1917. EXTENT_LOCKED);
  1918. spin_unlock(&inode->io_tree.lock);
  1919. if (state && state->start <= failrec->start &&
  1920. state->end >= failrec->start + failrec->len - 1) {
  1921. num_copies = btrfs_num_copies(fs_info, failrec->logical,
  1922. failrec->len);
  1923. if (num_copies > 1) {
  1924. repair_io_failure(inode, start, failrec->len,
  1925. failrec->logical, page,
  1926. pg_offset, failrec->failed_mirror);
  1927. }
  1928. }
  1929. out:
  1930. free_io_failure(inode, failrec);
  1931. return 0;
  1932. }
  1933. /*
  1934. * Can be called when
  1935. * - hold extent lock
  1936. * - under ordered extent
  1937. * - the inode is freeing
  1938. */
  1939. void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start, u64 end)
  1940. {
  1941. struct extent_io_tree *failure_tree = &inode->io_failure_tree;
  1942. struct io_failure_record *failrec;
  1943. struct extent_state *state, *next;
  1944. if (RB_EMPTY_ROOT(&failure_tree->state))
  1945. return;
  1946. spin_lock(&failure_tree->lock);
  1947. state = find_first_extent_bit_state(failure_tree, start, EXTENT_DIRTY);
  1948. while (state) {
  1949. if (state->start > end)
  1950. break;
  1951. ASSERT(state->end <= end);
  1952. next = next_state(state);
  1953. failrec = state->failrec;
  1954. free_extent_state(state);
  1955. kfree(failrec);
  1956. state = next;
  1957. }
  1958. spin_unlock(&failure_tree->lock);
  1959. }
  1960. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  1961. struct io_failure_record **failrec_ret)
  1962. {
  1963. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  1964. struct io_failure_record *failrec;
  1965. struct extent_map *em;
  1966. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1967. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  1968. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1969. int ret;
  1970. u64 logical;
  1971. ret = get_state_failrec(failure_tree, start, &failrec);
  1972. if (ret) {
  1973. failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
  1974. if (!failrec)
  1975. return -ENOMEM;
  1976. failrec->start = start;
  1977. failrec->len = end - start + 1;
  1978. failrec->this_mirror = 0;
  1979. failrec->bio_flags = 0;
  1980. failrec->in_validation = 0;
  1981. read_lock(&em_tree->lock);
  1982. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1983. if (!em) {
  1984. read_unlock(&em_tree->lock);
  1985. kfree(failrec);
  1986. return -EIO;
  1987. }
  1988. if (em->start > start || em->start + em->len <= start) {
  1989. free_extent_map(em);
  1990. em = NULL;
  1991. }
  1992. read_unlock(&em_tree->lock);
  1993. if (!em) {
  1994. kfree(failrec);
  1995. return -EIO;
  1996. }
  1997. logical = start - em->start;
  1998. logical = em->block_start + logical;
  1999. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2000. logical = em->block_start;
  2001. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  2002. extent_set_compress_type(&failrec->bio_flags,
  2003. em->compress_type);
  2004. }
  2005. btrfs_debug(fs_info,
  2006. "Get IO Failure Record: (new) logical=%llu, start=%llu, len=%llu",
  2007. logical, start, failrec->len);
  2008. failrec->logical = logical;
  2009. free_extent_map(em);
  2010. /* set the bits in the private failure tree */
  2011. ret = set_extent_bits(failure_tree, start, end,
  2012. EXTENT_LOCKED | EXTENT_DIRTY);
  2013. if (ret >= 0)
  2014. ret = set_state_failrec(failure_tree, start, failrec);
  2015. /* set the bits in the inode's tree */
  2016. if (ret >= 0)
  2017. ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED);
  2018. if (ret < 0) {
  2019. kfree(failrec);
  2020. return ret;
  2021. }
  2022. } else {
  2023. btrfs_debug(fs_info,
  2024. "Get IO Failure Record: (found) logical=%llu, start=%llu, len=%llu, validation=%d",
  2025. failrec->logical, failrec->start, failrec->len,
  2026. failrec->in_validation);
  2027. /*
  2028. * when data can be on disk more than twice, add to failrec here
  2029. * (e.g. with a list for failed_mirror) to make
  2030. * clean_io_failure() clean all those errors at once.
  2031. */
  2032. }
  2033. *failrec_ret = failrec;
  2034. return 0;
  2035. }
  2036. int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  2037. struct io_failure_record *failrec, int failed_mirror)
  2038. {
  2039. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2040. int num_copies;
  2041. num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
  2042. if (num_copies == 1) {
  2043. /*
  2044. * we only have a single copy of the data, so don't bother with
  2045. * all the retry and error correction code that follows. no
  2046. * matter what the error is, it is very likely to persist.
  2047. */
  2048. btrfs_debug(fs_info,
  2049. "Check Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
  2050. num_copies, failrec->this_mirror, failed_mirror);
  2051. return 0;
  2052. }
  2053. /*
  2054. * there are two premises:
  2055. * a) deliver good data to the caller
  2056. * b) correct the bad sectors on disk
  2057. */
  2058. if (failed_bio->bi_vcnt > 1) {
  2059. /*
  2060. * to fulfill b), we need to know the exact failing sectors, as
  2061. * we don't want to rewrite any more than the failed ones. thus,
  2062. * we need separate read requests for the failed bio
  2063. *
  2064. * if the following BUG_ON triggers, our validation request got
  2065. * merged. we need separate requests for our algorithm to work.
  2066. */
  2067. BUG_ON(failrec->in_validation);
  2068. failrec->in_validation = 1;
  2069. failrec->this_mirror = failed_mirror;
  2070. } else {
  2071. /*
  2072. * we're ready to fulfill a) and b) alongside. get a good copy
  2073. * of the failed sector and if we succeed, we have setup
  2074. * everything for repair_io_failure to do the rest for us.
  2075. */
  2076. if (failrec->in_validation) {
  2077. BUG_ON(failrec->this_mirror != failed_mirror);
  2078. failrec->in_validation = 0;
  2079. failrec->this_mirror = 0;
  2080. }
  2081. failrec->failed_mirror = failed_mirror;
  2082. failrec->this_mirror++;
  2083. if (failrec->this_mirror == failed_mirror)
  2084. failrec->this_mirror++;
  2085. }
  2086. if (failrec->this_mirror > num_copies) {
  2087. btrfs_debug(fs_info,
  2088. "Check Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
  2089. num_copies, failrec->this_mirror, failed_mirror);
  2090. return 0;
  2091. }
  2092. return 1;
  2093. }
  2094. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  2095. struct io_failure_record *failrec,
  2096. struct page *page, int pg_offset, int icsum,
  2097. bio_end_io_t *endio_func, void *data)
  2098. {
  2099. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2100. struct bio *bio;
  2101. struct btrfs_io_bio *btrfs_failed_bio;
  2102. struct btrfs_io_bio *btrfs_bio;
  2103. bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
  2104. if (!bio)
  2105. return NULL;
  2106. bio->bi_end_io = endio_func;
  2107. bio->bi_iter.bi_sector = failrec->logical >> 9;
  2108. bio->bi_bdev = fs_info->fs_devices->latest_bdev;
  2109. bio->bi_iter.bi_size = 0;
  2110. bio->bi_private = data;
  2111. btrfs_failed_bio = btrfs_io_bio(failed_bio);
  2112. if (btrfs_failed_bio->csum) {
  2113. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2114. btrfs_bio = btrfs_io_bio(bio);
  2115. btrfs_bio->csum = btrfs_bio->csum_inline;
  2116. icsum *= csum_size;
  2117. memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + icsum,
  2118. csum_size);
  2119. }
  2120. bio_add_page(bio, page, failrec->len, pg_offset);
  2121. return bio;
  2122. }
  2123. /*
  2124. * this is a generic handler for readpage errors (default
  2125. * readpage_io_failed_hook). if other copies exist, read those and write back
  2126. * good data to the failed position. does not investigate in remapping the
  2127. * failed extent elsewhere, hoping the device will be smart enough to do this as
  2128. * needed
  2129. */
  2130. static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
  2131. struct page *page, u64 start, u64 end,
  2132. int failed_mirror)
  2133. {
  2134. struct io_failure_record *failrec;
  2135. struct inode *inode = page->mapping->host;
  2136. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2137. struct bio *bio;
  2138. int read_mode = 0;
  2139. int ret;
  2140. BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
  2141. ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
  2142. if (ret)
  2143. return ret;
  2144. ret = btrfs_check_repairable(inode, failed_bio, failrec, failed_mirror);
  2145. if (!ret) {
  2146. free_io_failure(BTRFS_I(inode), failrec);
  2147. return -EIO;
  2148. }
  2149. if (failed_bio->bi_vcnt > 1)
  2150. read_mode |= REQ_FAILFAST_DEV;
  2151. phy_offset >>= inode->i_sb->s_blocksize_bits;
  2152. bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
  2153. start - page_offset(page),
  2154. (int)phy_offset, failed_bio->bi_end_io,
  2155. NULL);
  2156. if (!bio) {
  2157. free_io_failure(BTRFS_I(inode), failrec);
  2158. return -EIO;
  2159. }
  2160. bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
  2161. btrfs_debug(btrfs_sb(inode->i_sb),
  2162. "Repair Read Error: submitting new read[%#x] to this_mirror=%d, in_validation=%d",
  2163. read_mode, failrec->this_mirror, failrec->in_validation);
  2164. ret = tree->ops->submit_bio_hook(inode, bio, failrec->this_mirror,
  2165. failrec->bio_flags, 0);
  2166. if (ret) {
  2167. free_io_failure(BTRFS_I(inode), failrec);
  2168. bio_put(bio);
  2169. }
  2170. return ret;
  2171. }
  2172. /* lots and lots of room for performance fixes in the end_bio funcs */
  2173. void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
  2174. {
  2175. int uptodate = (err == 0);
  2176. struct extent_io_tree *tree;
  2177. int ret = 0;
  2178. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2179. if (tree->ops && tree->ops->writepage_end_io_hook)
  2180. tree->ops->writepage_end_io_hook(page, start, end, NULL,
  2181. uptodate);
  2182. if (!uptodate) {
  2183. ClearPageUptodate(page);
  2184. SetPageError(page);
  2185. ret = ret < 0 ? ret : -EIO;
  2186. mapping_set_error(page->mapping, ret);
  2187. }
  2188. }
  2189. /*
  2190. * after a writepage IO is done, we need to:
  2191. * clear the uptodate bits on error
  2192. * clear the writeback bits in the extent tree for this IO
  2193. * end_page_writeback if the page has no more pending IO
  2194. *
  2195. * Scheduling is not allowed, so the extent state tree is expected
  2196. * to have one and only one object corresponding to this IO.
  2197. */
  2198. static void end_bio_extent_writepage(struct bio *bio)
  2199. {
  2200. struct bio_vec *bvec;
  2201. u64 start;
  2202. u64 end;
  2203. int i;
  2204. bio_for_each_segment_all(bvec, bio, i) {
  2205. struct page *page = bvec->bv_page;
  2206. struct inode *inode = page->mapping->host;
  2207. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2208. /* We always issue full-page reads, but if some block
  2209. * in a page fails to read, blk_update_request() will
  2210. * advance bv_offset and adjust bv_len to compensate.
  2211. * Print a warning for nonzero offsets, and an error
  2212. * if they don't add up to a full page. */
  2213. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2214. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2215. btrfs_err(fs_info,
  2216. "partial page write in btrfs with offset %u and length %u",
  2217. bvec->bv_offset, bvec->bv_len);
  2218. else
  2219. btrfs_info(fs_info,
  2220. "incomplete page write in btrfs with offset %u and length %u",
  2221. bvec->bv_offset, bvec->bv_len);
  2222. }
  2223. start = page_offset(page);
  2224. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2225. end_extent_writepage(page, bio->bi_error, start, end);
  2226. end_page_writeback(page);
  2227. }
  2228. bio_put(bio);
  2229. }
  2230. static void
  2231. endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
  2232. int uptodate)
  2233. {
  2234. struct extent_state *cached = NULL;
  2235. u64 end = start + len - 1;
  2236. if (uptodate && tree->track_uptodate)
  2237. set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
  2238. unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
  2239. }
  2240. /*
  2241. * after a readpage IO is done, we need to:
  2242. * clear the uptodate bits on error
  2243. * set the uptodate bits if things worked
  2244. * set the page up to date if all extents in the tree are uptodate
  2245. * clear the lock bit in the extent tree
  2246. * unlock the page if there are no other extents locked for it
  2247. *
  2248. * Scheduling is not allowed, so the extent state tree is expected
  2249. * to have one and only one object corresponding to this IO.
  2250. */
  2251. static void end_bio_extent_readpage(struct bio *bio)
  2252. {
  2253. struct bio_vec *bvec;
  2254. int uptodate = !bio->bi_error;
  2255. struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
  2256. struct extent_io_tree *tree;
  2257. u64 offset = 0;
  2258. u64 start;
  2259. u64 end;
  2260. u64 len;
  2261. u64 extent_start = 0;
  2262. u64 extent_len = 0;
  2263. int mirror;
  2264. int ret;
  2265. int i;
  2266. bio_for_each_segment_all(bvec, bio, i) {
  2267. struct page *page = bvec->bv_page;
  2268. struct inode *inode = page->mapping->host;
  2269. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  2270. btrfs_debug(fs_info,
  2271. "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
  2272. (u64)bio->bi_iter.bi_sector, bio->bi_error,
  2273. io_bio->mirror_num);
  2274. tree = &BTRFS_I(inode)->io_tree;
  2275. /* We always issue full-page reads, but if some block
  2276. * in a page fails to read, blk_update_request() will
  2277. * advance bv_offset and adjust bv_len to compensate.
  2278. * Print a warning for nonzero offsets, and an error
  2279. * if they don't add up to a full page. */
  2280. if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
  2281. if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
  2282. btrfs_err(fs_info,
  2283. "partial page read in btrfs with offset %u and length %u",
  2284. bvec->bv_offset, bvec->bv_len);
  2285. else
  2286. btrfs_info(fs_info,
  2287. "incomplete page read in btrfs with offset %u and length %u",
  2288. bvec->bv_offset, bvec->bv_len);
  2289. }
  2290. start = page_offset(page);
  2291. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2292. len = bvec->bv_len;
  2293. mirror = io_bio->mirror_num;
  2294. if (likely(uptodate && tree->ops)) {
  2295. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2296. page, start, end,
  2297. mirror);
  2298. if (ret)
  2299. uptodate = 0;
  2300. else
  2301. clean_io_failure(BTRFS_I(inode), start,
  2302. page, 0);
  2303. }
  2304. if (likely(uptodate))
  2305. goto readpage_ok;
  2306. if (tree->ops) {
  2307. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2308. if (ret == -EAGAIN) {
  2309. /*
  2310. * Data inode's readpage_io_failed_hook() always
  2311. * returns -EAGAIN.
  2312. *
  2313. * The generic bio_readpage_error handles errors
  2314. * the following way: If possible, new read
  2315. * requests are created and submitted and will
  2316. * end up in end_bio_extent_readpage as well (if
  2317. * we're lucky, not in the !uptodate case). In
  2318. * that case it returns 0 and we just go on with
  2319. * the next page in our bio. If it can't handle
  2320. * the error it will return -EIO and we remain
  2321. * responsible for that page.
  2322. */
  2323. ret = bio_readpage_error(bio, offset, page,
  2324. start, end, mirror);
  2325. if (ret == 0) {
  2326. uptodate = !bio->bi_error;
  2327. offset += len;
  2328. continue;
  2329. }
  2330. }
  2331. /*
  2332. * metadata's readpage_io_failed_hook() always returns
  2333. * -EIO and fixes nothing. -EIO is also returned if
  2334. * data inode error could not be fixed.
  2335. */
  2336. ASSERT(ret == -EIO);
  2337. }
  2338. readpage_ok:
  2339. if (likely(uptodate)) {
  2340. loff_t i_size = i_size_read(inode);
  2341. pgoff_t end_index = i_size >> PAGE_SHIFT;
  2342. unsigned off;
  2343. /* Zero out the end if this page straddles i_size */
  2344. off = i_size & (PAGE_SIZE-1);
  2345. if (page->index == end_index && off)
  2346. zero_user_segment(page, off, PAGE_SIZE);
  2347. SetPageUptodate(page);
  2348. } else {
  2349. ClearPageUptodate(page);
  2350. SetPageError(page);
  2351. }
  2352. unlock_page(page);
  2353. offset += len;
  2354. if (unlikely(!uptodate)) {
  2355. if (extent_len) {
  2356. endio_readpage_release_extent(tree,
  2357. extent_start,
  2358. extent_len, 1);
  2359. extent_start = 0;
  2360. extent_len = 0;
  2361. }
  2362. endio_readpage_release_extent(tree, start,
  2363. end - start + 1, 0);
  2364. } else if (!extent_len) {
  2365. extent_start = start;
  2366. extent_len = end + 1 - start;
  2367. } else if (extent_start + extent_len == start) {
  2368. extent_len += end + 1 - start;
  2369. } else {
  2370. endio_readpage_release_extent(tree, extent_start,
  2371. extent_len, uptodate);
  2372. extent_start = start;
  2373. extent_len = end + 1 - start;
  2374. }
  2375. }
  2376. if (extent_len)
  2377. endio_readpage_release_extent(tree, extent_start, extent_len,
  2378. uptodate);
  2379. if (io_bio->end_io)
  2380. io_bio->end_io(io_bio, bio->bi_error);
  2381. bio_put(bio);
  2382. }
  2383. /*
  2384. * this allocates from the btrfs_bioset. We're returning a bio right now
  2385. * but you can call btrfs_io_bio for the appropriate container_of magic
  2386. */
  2387. struct bio *
  2388. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2389. gfp_t gfp_flags)
  2390. {
  2391. struct btrfs_io_bio *btrfs_bio;
  2392. struct bio *bio;
  2393. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2394. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2395. while (!bio && (nr_vecs /= 2)) {
  2396. bio = bio_alloc_bioset(gfp_flags,
  2397. nr_vecs, btrfs_bioset);
  2398. }
  2399. }
  2400. if (bio) {
  2401. bio->bi_bdev = bdev;
  2402. bio->bi_iter.bi_sector = first_sector;
  2403. btrfs_bio = btrfs_io_bio(bio);
  2404. btrfs_bio->csum = NULL;
  2405. btrfs_bio->csum_allocated = NULL;
  2406. btrfs_bio->end_io = NULL;
  2407. }
  2408. return bio;
  2409. }
  2410. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2411. {
  2412. struct btrfs_io_bio *btrfs_bio;
  2413. struct bio *new;
  2414. new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
  2415. if (new) {
  2416. btrfs_bio = btrfs_io_bio(new);
  2417. btrfs_bio->csum = NULL;
  2418. btrfs_bio->csum_allocated = NULL;
  2419. btrfs_bio->end_io = NULL;
  2420. }
  2421. return new;
  2422. }
  2423. /* this also allocates from the btrfs_bioset */
  2424. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2425. {
  2426. struct btrfs_io_bio *btrfs_bio;
  2427. struct bio *bio;
  2428. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2429. if (bio) {
  2430. btrfs_bio = btrfs_io_bio(bio);
  2431. btrfs_bio->csum = NULL;
  2432. btrfs_bio->csum_allocated = NULL;
  2433. btrfs_bio->end_io = NULL;
  2434. }
  2435. return bio;
  2436. }
  2437. static int __must_check submit_one_bio(struct bio *bio, int mirror_num,
  2438. unsigned long bio_flags)
  2439. {
  2440. int ret = 0;
  2441. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2442. struct page *page = bvec->bv_page;
  2443. struct extent_io_tree *tree = bio->bi_private;
  2444. u64 start;
  2445. start = page_offset(page) + bvec->bv_offset;
  2446. bio->bi_private = NULL;
  2447. bio_get(bio);
  2448. if (tree->ops)
  2449. ret = tree->ops->submit_bio_hook(page->mapping->host, bio,
  2450. mirror_num, bio_flags, start);
  2451. else
  2452. btrfsic_submit_bio(bio);
  2453. bio_put(bio);
  2454. return ret;
  2455. }
  2456. static int merge_bio(struct extent_io_tree *tree, struct page *page,
  2457. unsigned long offset, size_t size, struct bio *bio,
  2458. unsigned long bio_flags)
  2459. {
  2460. int ret = 0;
  2461. if (tree->ops)
  2462. ret = tree->ops->merge_bio_hook(page, offset, size, bio,
  2463. bio_flags);
  2464. return ret;
  2465. }
  2466. static int submit_extent_page(int op, int op_flags, struct extent_io_tree *tree,
  2467. struct writeback_control *wbc,
  2468. struct page *page, sector_t sector,
  2469. size_t size, unsigned long offset,
  2470. struct block_device *bdev,
  2471. struct bio **bio_ret,
  2472. bio_end_io_t end_io_func,
  2473. int mirror_num,
  2474. unsigned long prev_bio_flags,
  2475. unsigned long bio_flags,
  2476. bool force_bio_submit)
  2477. {
  2478. int ret = 0;
  2479. struct bio *bio;
  2480. int contig = 0;
  2481. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2482. size_t page_size = min_t(size_t, size, PAGE_SIZE);
  2483. if (bio_ret && *bio_ret) {
  2484. bio = *bio_ret;
  2485. if (old_compressed)
  2486. contig = bio->bi_iter.bi_sector == sector;
  2487. else
  2488. contig = bio_end_sector(bio) == sector;
  2489. if (prev_bio_flags != bio_flags || !contig ||
  2490. force_bio_submit ||
  2491. merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
  2492. bio_add_page(bio, page, page_size, offset) < page_size) {
  2493. ret = submit_one_bio(bio, mirror_num, prev_bio_flags);
  2494. if (ret < 0) {
  2495. *bio_ret = NULL;
  2496. return ret;
  2497. }
  2498. bio = NULL;
  2499. } else {
  2500. if (wbc)
  2501. wbc_account_io(wbc, page, page_size);
  2502. return 0;
  2503. }
  2504. }
  2505. bio = btrfs_bio_alloc(bdev, sector, BIO_MAX_PAGES,
  2506. GFP_NOFS | __GFP_HIGH);
  2507. if (!bio)
  2508. return -ENOMEM;
  2509. bio_add_page(bio, page, page_size, offset);
  2510. bio->bi_end_io = end_io_func;
  2511. bio->bi_private = tree;
  2512. bio_set_op_attrs(bio, op, op_flags);
  2513. if (wbc) {
  2514. wbc_init_bio(wbc, bio);
  2515. wbc_account_io(wbc, page, page_size);
  2516. }
  2517. if (bio_ret)
  2518. *bio_ret = bio;
  2519. else
  2520. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2521. return ret;
  2522. }
  2523. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2524. struct page *page)
  2525. {
  2526. if (!PagePrivate(page)) {
  2527. SetPagePrivate(page);
  2528. get_page(page);
  2529. set_page_private(page, (unsigned long)eb);
  2530. } else {
  2531. WARN_ON(page->private != (unsigned long)eb);
  2532. }
  2533. }
  2534. void set_page_extent_mapped(struct page *page)
  2535. {
  2536. if (!PagePrivate(page)) {
  2537. SetPagePrivate(page);
  2538. get_page(page);
  2539. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2540. }
  2541. }
  2542. static struct extent_map *
  2543. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2544. u64 start, u64 len, get_extent_t *get_extent,
  2545. struct extent_map **em_cached)
  2546. {
  2547. struct extent_map *em;
  2548. if (em_cached && *em_cached) {
  2549. em = *em_cached;
  2550. if (extent_map_in_tree(em) && start >= em->start &&
  2551. start < extent_map_end(em)) {
  2552. refcount_inc(&em->refs);
  2553. return em;
  2554. }
  2555. free_extent_map(em);
  2556. *em_cached = NULL;
  2557. }
  2558. em = get_extent(BTRFS_I(inode), page, pg_offset, start, len, 0);
  2559. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2560. BUG_ON(*em_cached);
  2561. refcount_inc(&em->refs);
  2562. *em_cached = em;
  2563. }
  2564. return em;
  2565. }
  2566. /*
  2567. * basic readpage implementation. Locked extent state structs are inserted
  2568. * into the tree that are removed when the IO is done (by the end_io
  2569. * handlers)
  2570. * XXX JDM: This needs looking at to ensure proper page locking
  2571. * return 0 on success, otherwise return error
  2572. */
  2573. static int __do_readpage(struct extent_io_tree *tree,
  2574. struct page *page,
  2575. get_extent_t *get_extent,
  2576. struct extent_map **em_cached,
  2577. struct bio **bio, int mirror_num,
  2578. unsigned long *bio_flags, int read_flags,
  2579. u64 *prev_em_start)
  2580. {
  2581. struct inode *inode = page->mapping->host;
  2582. u64 start = page_offset(page);
  2583. u64 page_end = start + PAGE_SIZE - 1;
  2584. u64 end;
  2585. u64 cur = start;
  2586. u64 extent_offset;
  2587. u64 last_byte = i_size_read(inode);
  2588. u64 block_start;
  2589. u64 cur_end;
  2590. sector_t sector;
  2591. struct extent_map *em;
  2592. struct block_device *bdev;
  2593. int ret = 0;
  2594. int nr = 0;
  2595. size_t pg_offset = 0;
  2596. size_t iosize;
  2597. size_t disk_io_size;
  2598. size_t blocksize = inode->i_sb->s_blocksize;
  2599. unsigned long this_bio_flag = 0;
  2600. set_page_extent_mapped(page);
  2601. end = page_end;
  2602. if (!PageUptodate(page)) {
  2603. if (cleancache_get_page(page) == 0) {
  2604. BUG_ON(blocksize != PAGE_SIZE);
  2605. unlock_extent(tree, start, end);
  2606. goto out;
  2607. }
  2608. }
  2609. if (page->index == last_byte >> PAGE_SHIFT) {
  2610. char *userpage;
  2611. size_t zero_offset = last_byte & (PAGE_SIZE - 1);
  2612. if (zero_offset) {
  2613. iosize = PAGE_SIZE - zero_offset;
  2614. userpage = kmap_atomic(page);
  2615. memset(userpage + zero_offset, 0, iosize);
  2616. flush_dcache_page(page);
  2617. kunmap_atomic(userpage);
  2618. }
  2619. }
  2620. while (cur <= end) {
  2621. bool force_bio_submit = false;
  2622. if (cur >= last_byte) {
  2623. char *userpage;
  2624. struct extent_state *cached = NULL;
  2625. iosize = PAGE_SIZE - pg_offset;
  2626. userpage = kmap_atomic(page);
  2627. memset(userpage + pg_offset, 0, iosize);
  2628. flush_dcache_page(page);
  2629. kunmap_atomic(userpage);
  2630. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2631. &cached, GFP_NOFS);
  2632. unlock_extent_cached(tree, cur,
  2633. cur + iosize - 1,
  2634. &cached, GFP_NOFS);
  2635. break;
  2636. }
  2637. em = __get_extent_map(inode, page, pg_offset, cur,
  2638. end - cur + 1, get_extent, em_cached);
  2639. if (IS_ERR_OR_NULL(em)) {
  2640. SetPageError(page);
  2641. unlock_extent(tree, cur, end);
  2642. break;
  2643. }
  2644. extent_offset = cur - em->start;
  2645. BUG_ON(extent_map_end(em) <= cur);
  2646. BUG_ON(end < cur);
  2647. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2648. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2649. extent_set_compress_type(&this_bio_flag,
  2650. em->compress_type);
  2651. }
  2652. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2653. cur_end = min(extent_map_end(em) - 1, end);
  2654. iosize = ALIGN(iosize, blocksize);
  2655. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2656. disk_io_size = em->block_len;
  2657. sector = em->block_start >> 9;
  2658. } else {
  2659. sector = (em->block_start + extent_offset) >> 9;
  2660. disk_io_size = iosize;
  2661. }
  2662. bdev = em->bdev;
  2663. block_start = em->block_start;
  2664. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2665. block_start = EXTENT_MAP_HOLE;
  2666. /*
  2667. * If we have a file range that points to a compressed extent
  2668. * and it's followed by a consecutive file range that points to
  2669. * to the same compressed extent (possibly with a different
  2670. * offset and/or length, so it either points to the whole extent
  2671. * or only part of it), we must make sure we do not submit a
  2672. * single bio to populate the pages for the 2 ranges because
  2673. * this makes the compressed extent read zero out the pages
  2674. * belonging to the 2nd range. Imagine the following scenario:
  2675. *
  2676. * File layout
  2677. * [0 - 8K] [8K - 24K]
  2678. * | |
  2679. * | |
  2680. * points to extent X, points to extent X,
  2681. * offset 4K, length of 8K offset 0, length 16K
  2682. *
  2683. * [extent X, compressed length = 4K uncompressed length = 16K]
  2684. *
  2685. * If the bio to read the compressed extent covers both ranges,
  2686. * it will decompress extent X into the pages belonging to the
  2687. * first range and then it will stop, zeroing out the remaining
  2688. * pages that belong to the other range that points to extent X.
  2689. * So here we make sure we submit 2 bios, one for the first
  2690. * range and another one for the third range. Both will target
  2691. * the same physical extent from disk, but we can't currently
  2692. * make the compressed bio endio callback populate the pages
  2693. * for both ranges because each compressed bio is tightly
  2694. * coupled with a single extent map, and each range can have
  2695. * an extent map with a different offset value relative to the
  2696. * uncompressed data of our extent and different lengths. This
  2697. * is a corner case so we prioritize correctness over
  2698. * non-optimal behavior (submitting 2 bios for the same extent).
  2699. */
  2700. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) &&
  2701. prev_em_start && *prev_em_start != (u64)-1 &&
  2702. *prev_em_start != em->orig_start)
  2703. force_bio_submit = true;
  2704. if (prev_em_start)
  2705. *prev_em_start = em->orig_start;
  2706. free_extent_map(em);
  2707. em = NULL;
  2708. /* we've found a hole, just zero and go on */
  2709. if (block_start == EXTENT_MAP_HOLE) {
  2710. char *userpage;
  2711. struct extent_state *cached = NULL;
  2712. userpage = kmap_atomic(page);
  2713. memset(userpage + pg_offset, 0, iosize);
  2714. flush_dcache_page(page);
  2715. kunmap_atomic(userpage);
  2716. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2717. &cached, GFP_NOFS);
  2718. unlock_extent_cached(tree, cur,
  2719. cur + iosize - 1,
  2720. &cached, GFP_NOFS);
  2721. cur = cur + iosize;
  2722. pg_offset += iosize;
  2723. continue;
  2724. }
  2725. /* the get_extent function already copied into the page */
  2726. if (test_range_bit(tree, cur, cur_end,
  2727. EXTENT_UPTODATE, 1, NULL)) {
  2728. check_page_uptodate(tree, page);
  2729. unlock_extent(tree, cur, cur + iosize - 1);
  2730. cur = cur + iosize;
  2731. pg_offset += iosize;
  2732. continue;
  2733. }
  2734. /* we have an inline extent but it didn't get marked up
  2735. * to date. Error out
  2736. */
  2737. if (block_start == EXTENT_MAP_INLINE) {
  2738. SetPageError(page);
  2739. unlock_extent(tree, cur, cur + iosize - 1);
  2740. cur = cur + iosize;
  2741. pg_offset += iosize;
  2742. continue;
  2743. }
  2744. ret = submit_extent_page(REQ_OP_READ, read_flags, tree, NULL,
  2745. page, sector, disk_io_size, pg_offset,
  2746. bdev, bio,
  2747. end_bio_extent_readpage, mirror_num,
  2748. *bio_flags,
  2749. this_bio_flag,
  2750. force_bio_submit);
  2751. if (!ret) {
  2752. nr++;
  2753. *bio_flags = this_bio_flag;
  2754. } else {
  2755. SetPageError(page);
  2756. unlock_extent(tree, cur, cur + iosize - 1);
  2757. goto out;
  2758. }
  2759. cur = cur + iosize;
  2760. pg_offset += iosize;
  2761. }
  2762. out:
  2763. if (!nr) {
  2764. if (!PageError(page))
  2765. SetPageUptodate(page);
  2766. unlock_page(page);
  2767. }
  2768. return ret;
  2769. }
  2770. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2771. struct page *pages[], int nr_pages,
  2772. u64 start, u64 end,
  2773. get_extent_t *get_extent,
  2774. struct extent_map **em_cached,
  2775. struct bio **bio, int mirror_num,
  2776. unsigned long *bio_flags,
  2777. u64 *prev_em_start)
  2778. {
  2779. struct inode *inode;
  2780. struct btrfs_ordered_extent *ordered;
  2781. int index;
  2782. inode = pages[0]->mapping->host;
  2783. while (1) {
  2784. lock_extent(tree, start, end);
  2785. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2786. end - start + 1);
  2787. if (!ordered)
  2788. break;
  2789. unlock_extent(tree, start, end);
  2790. btrfs_start_ordered_extent(inode, ordered, 1);
  2791. btrfs_put_ordered_extent(ordered);
  2792. }
  2793. for (index = 0; index < nr_pages; index++) {
  2794. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2795. mirror_num, bio_flags, 0, prev_em_start);
  2796. put_page(pages[index]);
  2797. }
  2798. }
  2799. static void __extent_readpages(struct extent_io_tree *tree,
  2800. struct page *pages[],
  2801. int nr_pages, get_extent_t *get_extent,
  2802. struct extent_map **em_cached,
  2803. struct bio **bio, int mirror_num,
  2804. unsigned long *bio_flags,
  2805. u64 *prev_em_start)
  2806. {
  2807. u64 start = 0;
  2808. u64 end = 0;
  2809. u64 page_start;
  2810. int index;
  2811. int first_index = 0;
  2812. for (index = 0; index < nr_pages; index++) {
  2813. page_start = page_offset(pages[index]);
  2814. if (!end) {
  2815. start = page_start;
  2816. end = start + PAGE_SIZE - 1;
  2817. first_index = index;
  2818. } else if (end + 1 == page_start) {
  2819. end += PAGE_SIZE;
  2820. } else {
  2821. __do_contiguous_readpages(tree, &pages[first_index],
  2822. index - first_index, start,
  2823. end, get_extent, em_cached,
  2824. bio, mirror_num, bio_flags,
  2825. prev_em_start);
  2826. start = page_start;
  2827. end = start + PAGE_SIZE - 1;
  2828. first_index = index;
  2829. }
  2830. }
  2831. if (end)
  2832. __do_contiguous_readpages(tree, &pages[first_index],
  2833. index - first_index, start,
  2834. end, get_extent, em_cached, bio,
  2835. mirror_num, bio_flags,
  2836. prev_em_start);
  2837. }
  2838. static int __extent_read_full_page(struct extent_io_tree *tree,
  2839. struct page *page,
  2840. get_extent_t *get_extent,
  2841. struct bio **bio, int mirror_num,
  2842. unsigned long *bio_flags, int read_flags)
  2843. {
  2844. struct inode *inode = page->mapping->host;
  2845. struct btrfs_ordered_extent *ordered;
  2846. u64 start = page_offset(page);
  2847. u64 end = start + PAGE_SIZE - 1;
  2848. int ret;
  2849. while (1) {
  2850. lock_extent(tree, start, end);
  2851. ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
  2852. PAGE_SIZE);
  2853. if (!ordered)
  2854. break;
  2855. unlock_extent(tree, start, end);
  2856. btrfs_start_ordered_extent(inode, ordered, 1);
  2857. btrfs_put_ordered_extent(ordered);
  2858. }
  2859. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2860. bio_flags, read_flags, NULL);
  2861. return ret;
  2862. }
  2863. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2864. get_extent_t *get_extent, int mirror_num)
  2865. {
  2866. struct bio *bio = NULL;
  2867. unsigned long bio_flags = 0;
  2868. int ret;
  2869. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2870. &bio_flags, 0);
  2871. if (bio)
  2872. ret = submit_one_bio(bio, mirror_num, bio_flags);
  2873. return ret;
  2874. }
  2875. static void update_nr_written(struct writeback_control *wbc,
  2876. unsigned long nr_written)
  2877. {
  2878. wbc->nr_to_write -= nr_written;
  2879. }
  2880. /*
  2881. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2882. *
  2883. * This returns 1 if our fill_delalloc function did all the work required
  2884. * to write the page (copy into inline extent). In this case the IO has
  2885. * been started and the page is already unlocked.
  2886. *
  2887. * This returns 0 if all went well (page still locked)
  2888. * This returns < 0 if there were errors (page still locked)
  2889. */
  2890. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2891. struct page *page, struct writeback_control *wbc,
  2892. struct extent_page_data *epd,
  2893. u64 delalloc_start,
  2894. unsigned long *nr_written)
  2895. {
  2896. struct extent_io_tree *tree = epd->tree;
  2897. u64 page_end = delalloc_start + PAGE_SIZE - 1;
  2898. u64 nr_delalloc;
  2899. u64 delalloc_to_write = 0;
  2900. u64 delalloc_end = 0;
  2901. int ret;
  2902. int page_started = 0;
  2903. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2904. return 0;
  2905. while (delalloc_end < page_end) {
  2906. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2907. page,
  2908. &delalloc_start,
  2909. &delalloc_end,
  2910. BTRFS_MAX_EXTENT_SIZE);
  2911. if (nr_delalloc == 0) {
  2912. delalloc_start = delalloc_end + 1;
  2913. continue;
  2914. }
  2915. ret = tree->ops->fill_delalloc(inode, page,
  2916. delalloc_start,
  2917. delalloc_end,
  2918. &page_started,
  2919. nr_written);
  2920. /* File system has been set read-only */
  2921. if (ret) {
  2922. SetPageError(page);
  2923. /* fill_delalloc should be return < 0 for error
  2924. * but just in case, we use > 0 here meaning the
  2925. * IO is started, so we don't want to return > 0
  2926. * unless things are going well.
  2927. */
  2928. ret = ret < 0 ? ret : -EIO;
  2929. goto done;
  2930. }
  2931. /*
  2932. * delalloc_end is already one less than the total length, so
  2933. * we don't subtract one from PAGE_SIZE
  2934. */
  2935. delalloc_to_write += (delalloc_end - delalloc_start +
  2936. PAGE_SIZE) >> PAGE_SHIFT;
  2937. delalloc_start = delalloc_end + 1;
  2938. }
  2939. if (wbc->nr_to_write < delalloc_to_write) {
  2940. int thresh = 8192;
  2941. if (delalloc_to_write < thresh * 2)
  2942. thresh = delalloc_to_write;
  2943. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2944. thresh);
  2945. }
  2946. /* did the fill delalloc function already unlock and start
  2947. * the IO?
  2948. */
  2949. if (page_started) {
  2950. /*
  2951. * we've unlocked the page, so we can't update
  2952. * the mapping's writeback index, just update
  2953. * nr_to_write.
  2954. */
  2955. wbc->nr_to_write -= *nr_written;
  2956. return 1;
  2957. }
  2958. ret = 0;
  2959. done:
  2960. return ret;
  2961. }
  2962. /*
  2963. * helper for __extent_writepage. This calls the writepage start hooks,
  2964. * and does the loop to map the page into extents and bios.
  2965. *
  2966. * We return 1 if the IO is started and the page is unlocked,
  2967. * 0 if all went well (page still locked)
  2968. * < 0 if there were errors (page still locked)
  2969. */
  2970. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2971. struct page *page,
  2972. struct writeback_control *wbc,
  2973. struct extent_page_data *epd,
  2974. loff_t i_size,
  2975. unsigned long nr_written,
  2976. int write_flags, int *nr_ret)
  2977. {
  2978. struct extent_io_tree *tree = epd->tree;
  2979. u64 start = page_offset(page);
  2980. u64 page_end = start + PAGE_SIZE - 1;
  2981. u64 end;
  2982. u64 cur = start;
  2983. u64 extent_offset;
  2984. u64 block_start;
  2985. u64 iosize;
  2986. sector_t sector;
  2987. struct extent_map *em;
  2988. struct block_device *bdev;
  2989. size_t pg_offset = 0;
  2990. size_t blocksize;
  2991. int ret = 0;
  2992. int nr = 0;
  2993. bool compressed;
  2994. if (tree->ops && tree->ops->writepage_start_hook) {
  2995. ret = tree->ops->writepage_start_hook(page, start,
  2996. page_end);
  2997. if (ret) {
  2998. /* Fixup worker will requeue */
  2999. if (ret == -EBUSY)
  3000. wbc->pages_skipped++;
  3001. else
  3002. redirty_page_for_writepage(wbc, page);
  3003. update_nr_written(wbc, nr_written);
  3004. unlock_page(page);
  3005. return 1;
  3006. }
  3007. }
  3008. /*
  3009. * we don't want to touch the inode after unlocking the page,
  3010. * so we update the mapping writeback index now
  3011. */
  3012. update_nr_written(wbc, nr_written + 1);
  3013. end = page_end;
  3014. if (i_size <= start) {
  3015. if (tree->ops && tree->ops->writepage_end_io_hook)
  3016. tree->ops->writepage_end_io_hook(page, start,
  3017. page_end, NULL, 1);
  3018. goto done;
  3019. }
  3020. blocksize = inode->i_sb->s_blocksize;
  3021. while (cur <= end) {
  3022. u64 em_end;
  3023. if (cur >= i_size) {
  3024. if (tree->ops && tree->ops->writepage_end_io_hook)
  3025. tree->ops->writepage_end_io_hook(page, cur,
  3026. page_end, NULL, 1);
  3027. break;
  3028. }
  3029. em = epd->get_extent(BTRFS_I(inode), page, pg_offset, cur,
  3030. end - cur + 1, 1);
  3031. if (IS_ERR_OR_NULL(em)) {
  3032. SetPageError(page);
  3033. ret = PTR_ERR_OR_ZERO(em);
  3034. break;
  3035. }
  3036. extent_offset = cur - em->start;
  3037. em_end = extent_map_end(em);
  3038. BUG_ON(em_end <= cur);
  3039. BUG_ON(end < cur);
  3040. iosize = min(em_end - cur, end - cur + 1);
  3041. iosize = ALIGN(iosize, blocksize);
  3042. sector = (em->block_start + extent_offset) >> 9;
  3043. bdev = em->bdev;
  3044. block_start = em->block_start;
  3045. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3046. free_extent_map(em);
  3047. em = NULL;
  3048. /*
  3049. * compressed and inline extents are written through other
  3050. * paths in the FS
  3051. */
  3052. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3053. block_start == EXTENT_MAP_INLINE) {
  3054. /*
  3055. * end_io notification does not happen here for
  3056. * compressed extents
  3057. */
  3058. if (!compressed && tree->ops &&
  3059. tree->ops->writepage_end_io_hook)
  3060. tree->ops->writepage_end_io_hook(page, cur,
  3061. cur + iosize - 1,
  3062. NULL, 1);
  3063. else if (compressed) {
  3064. /* we don't want to end_page_writeback on
  3065. * a compressed extent. this happens
  3066. * elsewhere
  3067. */
  3068. nr++;
  3069. }
  3070. cur += iosize;
  3071. pg_offset += iosize;
  3072. continue;
  3073. }
  3074. set_range_writeback(tree, cur, cur + iosize - 1);
  3075. if (!PageWriteback(page)) {
  3076. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3077. "page %lu not writeback, cur %llu end %llu",
  3078. page->index, cur, end);
  3079. }
  3080. ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
  3081. page, sector, iosize, pg_offset,
  3082. bdev, &epd->bio,
  3083. end_bio_extent_writepage,
  3084. 0, 0, 0, false);
  3085. if (ret) {
  3086. SetPageError(page);
  3087. if (PageWriteback(page))
  3088. end_page_writeback(page);
  3089. }
  3090. cur = cur + iosize;
  3091. pg_offset += iosize;
  3092. nr++;
  3093. }
  3094. done:
  3095. *nr_ret = nr;
  3096. return ret;
  3097. }
  3098. /*
  3099. * the writepage semantics are similar to regular writepage. extent
  3100. * records are inserted to lock ranges in the tree, and as dirty areas
  3101. * are found, they are marked writeback. Then the lock bits are removed
  3102. * and the end_io handler clears the writeback ranges
  3103. */
  3104. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3105. void *data)
  3106. {
  3107. struct inode *inode = page->mapping->host;
  3108. struct extent_page_data *epd = data;
  3109. u64 start = page_offset(page);
  3110. u64 page_end = start + PAGE_SIZE - 1;
  3111. int ret;
  3112. int nr = 0;
  3113. size_t pg_offset = 0;
  3114. loff_t i_size = i_size_read(inode);
  3115. unsigned long end_index = i_size >> PAGE_SHIFT;
  3116. int write_flags = 0;
  3117. unsigned long nr_written = 0;
  3118. if (wbc->sync_mode == WB_SYNC_ALL)
  3119. write_flags = REQ_SYNC;
  3120. trace___extent_writepage(page, inode, wbc);
  3121. WARN_ON(!PageLocked(page));
  3122. ClearPageError(page);
  3123. pg_offset = i_size & (PAGE_SIZE - 1);
  3124. if (page->index > end_index ||
  3125. (page->index == end_index && !pg_offset)) {
  3126. page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
  3127. unlock_page(page);
  3128. return 0;
  3129. }
  3130. if (page->index == end_index) {
  3131. char *userpage;
  3132. userpage = kmap_atomic(page);
  3133. memset(userpage + pg_offset, 0,
  3134. PAGE_SIZE - pg_offset);
  3135. kunmap_atomic(userpage);
  3136. flush_dcache_page(page);
  3137. }
  3138. pg_offset = 0;
  3139. set_page_extent_mapped(page);
  3140. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3141. if (ret == 1)
  3142. goto done_unlocked;
  3143. if (ret)
  3144. goto done;
  3145. ret = __extent_writepage_io(inode, page, wbc, epd,
  3146. i_size, nr_written, write_flags, &nr);
  3147. if (ret == 1)
  3148. goto done_unlocked;
  3149. done:
  3150. if (nr == 0) {
  3151. /* make sure the mapping tag for page dirty gets cleared */
  3152. set_page_writeback(page);
  3153. end_page_writeback(page);
  3154. }
  3155. if (PageError(page)) {
  3156. ret = ret < 0 ? ret : -EIO;
  3157. end_extent_writepage(page, ret, start, page_end);
  3158. }
  3159. unlock_page(page);
  3160. return ret;
  3161. done_unlocked:
  3162. return 0;
  3163. }
  3164. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3165. {
  3166. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3167. TASK_UNINTERRUPTIBLE);
  3168. }
  3169. static noinline_for_stack int
  3170. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3171. struct btrfs_fs_info *fs_info,
  3172. struct extent_page_data *epd)
  3173. {
  3174. unsigned long i, num_pages;
  3175. int flush = 0;
  3176. int ret = 0;
  3177. if (!btrfs_try_tree_write_lock(eb)) {
  3178. flush = 1;
  3179. flush_write_bio(epd);
  3180. btrfs_tree_lock(eb);
  3181. }
  3182. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3183. btrfs_tree_unlock(eb);
  3184. if (!epd->sync_io)
  3185. return 0;
  3186. if (!flush) {
  3187. flush_write_bio(epd);
  3188. flush = 1;
  3189. }
  3190. while (1) {
  3191. wait_on_extent_buffer_writeback(eb);
  3192. btrfs_tree_lock(eb);
  3193. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3194. break;
  3195. btrfs_tree_unlock(eb);
  3196. }
  3197. }
  3198. /*
  3199. * We need to do this to prevent races in people who check if the eb is
  3200. * under IO since we can end up having no IO bits set for a short period
  3201. * of time.
  3202. */
  3203. spin_lock(&eb->refs_lock);
  3204. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3205. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3206. spin_unlock(&eb->refs_lock);
  3207. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3208. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3209. -eb->len,
  3210. fs_info->dirty_metadata_batch);
  3211. ret = 1;
  3212. } else {
  3213. spin_unlock(&eb->refs_lock);
  3214. }
  3215. btrfs_tree_unlock(eb);
  3216. if (!ret)
  3217. return ret;
  3218. num_pages = num_extent_pages(eb->start, eb->len);
  3219. for (i = 0; i < num_pages; i++) {
  3220. struct page *p = eb->pages[i];
  3221. if (!trylock_page(p)) {
  3222. if (!flush) {
  3223. flush_write_bio(epd);
  3224. flush = 1;
  3225. }
  3226. lock_page(p);
  3227. }
  3228. }
  3229. return ret;
  3230. }
  3231. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3232. {
  3233. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3234. smp_mb__after_atomic();
  3235. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3236. }
  3237. static void set_btree_ioerr(struct page *page)
  3238. {
  3239. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3240. SetPageError(page);
  3241. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3242. return;
  3243. /*
  3244. * If writeback for a btree extent that doesn't belong to a log tree
  3245. * failed, increment the counter transaction->eb_write_errors.
  3246. * We do this because while the transaction is running and before it's
  3247. * committing (when we call filemap_fdata[write|wait]_range against
  3248. * the btree inode), we might have
  3249. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3250. * returns an error or an error happens during writeback, when we're
  3251. * committing the transaction we wouldn't know about it, since the pages
  3252. * can be no longer dirty nor marked anymore for writeback (if a
  3253. * subsequent modification to the extent buffer didn't happen before the
  3254. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3255. * able to find the pages tagged with SetPageError at transaction
  3256. * commit time. So if this happens we must abort the transaction,
  3257. * otherwise we commit a super block with btree roots that point to
  3258. * btree nodes/leafs whose content on disk is invalid - either garbage
  3259. * or the content of some node/leaf from a past generation that got
  3260. * cowed or deleted and is no longer valid.
  3261. *
  3262. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3263. * not be enough - we need to distinguish between log tree extents vs
  3264. * non-log tree extents, and the next filemap_fdatawait_range() call
  3265. * will catch and clear such errors in the mapping - and that call might
  3266. * be from a log sync and not from a transaction commit. Also, checking
  3267. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3268. * not done and would not be reliable - the eb might have been released
  3269. * from memory and reading it back again means that flag would not be
  3270. * set (since it's a runtime flag, not persisted on disk).
  3271. *
  3272. * Using the flags below in the btree inode also makes us achieve the
  3273. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3274. * writeback for all dirty pages and before filemap_fdatawait_range()
  3275. * is called, the writeback for all dirty pages had already finished
  3276. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3277. * filemap_fdatawait_range() would return success, as it could not know
  3278. * that writeback errors happened (the pages were no longer tagged for
  3279. * writeback).
  3280. */
  3281. switch (eb->log_index) {
  3282. case -1:
  3283. set_bit(BTRFS_FS_BTREE_ERR, &eb->fs_info->flags);
  3284. break;
  3285. case 0:
  3286. set_bit(BTRFS_FS_LOG1_ERR, &eb->fs_info->flags);
  3287. break;
  3288. case 1:
  3289. set_bit(BTRFS_FS_LOG2_ERR, &eb->fs_info->flags);
  3290. break;
  3291. default:
  3292. BUG(); /* unexpected, logic error */
  3293. }
  3294. }
  3295. static void end_bio_extent_buffer_writepage(struct bio *bio)
  3296. {
  3297. struct bio_vec *bvec;
  3298. struct extent_buffer *eb;
  3299. int i, done;
  3300. bio_for_each_segment_all(bvec, bio, i) {
  3301. struct page *page = bvec->bv_page;
  3302. eb = (struct extent_buffer *)page->private;
  3303. BUG_ON(!eb);
  3304. done = atomic_dec_and_test(&eb->io_pages);
  3305. if (bio->bi_error ||
  3306. test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3307. ClearPageUptodate(page);
  3308. set_btree_ioerr(page);
  3309. }
  3310. end_page_writeback(page);
  3311. if (!done)
  3312. continue;
  3313. end_extent_buffer_writeback(eb);
  3314. }
  3315. bio_put(bio);
  3316. }
  3317. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3318. struct btrfs_fs_info *fs_info,
  3319. struct writeback_control *wbc,
  3320. struct extent_page_data *epd)
  3321. {
  3322. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3323. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3324. u64 offset = eb->start;
  3325. u32 nritems;
  3326. unsigned long i, num_pages;
  3327. unsigned long bio_flags = 0;
  3328. unsigned long start, end;
  3329. int write_flags = (epd->sync_io ? REQ_SYNC : 0) | REQ_META;
  3330. int ret = 0;
  3331. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3332. num_pages = num_extent_pages(eb->start, eb->len);
  3333. atomic_set(&eb->io_pages, num_pages);
  3334. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3335. bio_flags = EXTENT_BIO_TREE_LOG;
  3336. /* set btree blocks beyond nritems with 0 to avoid stale content. */
  3337. nritems = btrfs_header_nritems(eb);
  3338. if (btrfs_header_level(eb) > 0) {
  3339. end = btrfs_node_key_ptr_offset(nritems);
  3340. memzero_extent_buffer(eb, end, eb->len - end);
  3341. } else {
  3342. /*
  3343. * leaf:
  3344. * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
  3345. */
  3346. start = btrfs_item_nr_offset(nritems);
  3347. end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb);
  3348. memzero_extent_buffer(eb, start, end - start);
  3349. }
  3350. for (i = 0; i < num_pages; i++) {
  3351. struct page *p = eb->pages[i];
  3352. clear_page_dirty_for_io(p);
  3353. set_page_writeback(p);
  3354. ret = submit_extent_page(REQ_OP_WRITE, write_flags, tree, wbc,
  3355. p, offset >> 9, PAGE_SIZE, 0, bdev,
  3356. &epd->bio,
  3357. end_bio_extent_buffer_writepage,
  3358. 0, epd->bio_flags, bio_flags, false);
  3359. epd->bio_flags = bio_flags;
  3360. if (ret) {
  3361. set_btree_ioerr(p);
  3362. if (PageWriteback(p))
  3363. end_page_writeback(p);
  3364. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3365. end_extent_buffer_writeback(eb);
  3366. ret = -EIO;
  3367. break;
  3368. }
  3369. offset += PAGE_SIZE;
  3370. update_nr_written(wbc, 1);
  3371. unlock_page(p);
  3372. }
  3373. if (unlikely(ret)) {
  3374. for (; i < num_pages; i++) {
  3375. struct page *p = eb->pages[i];
  3376. clear_page_dirty_for_io(p);
  3377. unlock_page(p);
  3378. }
  3379. }
  3380. return ret;
  3381. }
  3382. int btree_write_cache_pages(struct address_space *mapping,
  3383. struct writeback_control *wbc)
  3384. {
  3385. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3386. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3387. struct extent_buffer *eb, *prev_eb = NULL;
  3388. struct extent_page_data epd = {
  3389. .bio = NULL,
  3390. .tree = tree,
  3391. .extent_locked = 0,
  3392. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3393. .bio_flags = 0,
  3394. };
  3395. int ret = 0;
  3396. int done = 0;
  3397. int nr_to_write_done = 0;
  3398. struct pagevec pvec;
  3399. int nr_pages;
  3400. pgoff_t index;
  3401. pgoff_t end; /* Inclusive */
  3402. int scanned = 0;
  3403. int tag;
  3404. pagevec_init(&pvec, 0);
  3405. if (wbc->range_cyclic) {
  3406. index = mapping->writeback_index; /* Start from prev offset */
  3407. end = -1;
  3408. } else {
  3409. index = wbc->range_start >> PAGE_SHIFT;
  3410. end = wbc->range_end >> PAGE_SHIFT;
  3411. scanned = 1;
  3412. }
  3413. if (wbc->sync_mode == WB_SYNC_ALL)
  3414. tag = PAGECACHE_TAG_TOWRITE;
  3415. else
  3416. tag = PAGECACHE_TAG_DIRTY;
  3417. retry:
  3418. if (wbc->sync_mode == WB_SYNC_ALL)
  3419. tag_pages_for_writeback(mapping, index, end);
  3420. while (!done && !nr_to_write_done && (index <= end) &&
  3421. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3422. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3423. unsigned i;
  3424. scanned = 1;
  3425. for (i = 0; i < nr_pages; i++) {
  3426. struct page *page = pvec.pages[i];
  3427. if (!PagePrivate(page))
  3428. continue;
  3429. if (!wbc->range_cyclic && page->index > end) {
  3430. done = 1;
  3431. break;
  3432. }
  3433. spin_lock(&mapping->private_lock);
  3434. if (!PagePrivate(page)) {
  3435. spin_unlock(&mapping->private_lock);
  3436. continue;
  3437. }
  3438. eb = (struct extent_buffer *)page->private;
  3439. /*
  3440. * Shouldn't happen and normally this would be a BUG_ON
  3441. * but no sense in crashing the users box for something
  3442. * we can survive anyway.
  3443. */
  3444. if (WARN_ON(!eb)) {
  3445. spin_unlock(&mapping->private_lock);
  3446. continue;
  3447. }
  3448. if (eb == prev_eb) {
  3449. spin_unlock(&mapping->private_lock);
  3450. continue;
  3451. }
  3452. ret = atomic_inc_not_zero(&eb->refs);
  3453. spin_unlock(&mapping->private_lock);
  3454. if (!ret)
  3455. continue;
  3456. prev_eb = eb;
  3457. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3458. if (!ret) {
  3459. free_extent_buffer(eb);
  3460. continue;
  3461. }
  3462. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3463. if (ret) {
  3464. done = 1;
  3465. free_extent_buffer(eb);
  3466. break;
  3467. }
  3468. free_extent_buffer(eb);
  3469. /*
  3470. * the filesystem may choose to bump up nr_to_write.
  3471. * We have to make sure to honor the new nr_to_write
  3472. * at any time
  3473. */
  3474. nr_to_write_done = wbc->nr_to_write <= 0;
  3475. }
  3476. pagevec_release(&pvec);
  3477. cond_resched();
  3478. }
  3479. if (!scanned && !done) {
  3480. /*
  3481. * We hit the last page and there is more work to be done: wrap
  3482. * back to the start of the file
  3483. */
  3484. scanned = 1;
  3485. index = 0;
  3486. goto retry;
  3487. }
  3488. flush_write_bio(&epd);
  3489. return ret;
  3490. }
  3491. /**
  3492. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3493. * @mapping: address space structure to write
  3494. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3495. * @writepage: function called for each page
  3496. * @data: data passed to writepage function
  3497. *
  3498. * If a page is already under I/O, write_cache_pages() skips it, even
  3499. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3500. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3501. * and msync() need to guarantee that all the data which was dirty at the time
  3502. * the call was made get new I/O started against them. If wbc->sync_mode is
  3503. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3504. * existing IO to complete.
  3505. */
  3506. static int extent_write_cache_pages(struct address_space *mapping,
  3507. struct writeback_control *wbc,
  3508. writepage_t writepage, void *data,
  3509. void (*flush_fn)(void *))
  3510. {
  3511. struct inode *inode = mapping->host;
  3512. int ret = 0;
  3513. int done = 0;
  3514. int nr_to_write_done = 0;
  3515. struct pagevec pvec;
  3516. int nr_pages;
  3517. pgoff_t index;
  3518. pgoff_t end; /* Inclusive */
  3519. pgoff_t done_index;
  3520. int range_whole = 0;
  3521. int scanned = 0;
  3522. int tag;
  3523. /*
  3524. * We have to hold onto the inode so that ordered extents can do their
  3525. * work when the IO finishes. The alternative to this is failing to add
  3526. * an ordered extent if the igrab() fails there and that is a huge pain
  3527. * to deal with, so instead just hold onto the inode throughout the
  3528. * writepages operation. If it fails here we are freeing up the inode
  3529. * anyway and we'd rather not waste our time writing out stuff that is
  3530. * going to be truncated anyway.
  3531. */
  3532. if (!igrab(inode))
  3533. return 0;
  3534. pagevec_init(&pvec, 0);
  3535. if (wbc->range_cyclic) {
  3536. index = mapping->writeback_index; /* Start from prev offset */
  3537. end = -1;
  3538. } else {
  3539. index = wbc->range_start >> PAGE_SHIFT;
  3540. end = wbc->range_end >> PAGE_SHIFT;
  3541. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  3542. range_whole = 1;
  3543. scanned = 1;
  3544. }
  3545. if (wbc->sync_mode == WB_SYNC_ALL)
  3546. tag = PAGECACHE_TAG_TOWRITE;
  3547. else
  3548. tag = PAGECACHE_TAG_DIRTY;
  3549. retry:
  3550. if (wbc->sync_mode == WB_SYNC_ALL)
  3551. tag_pages_for_writeback(mapping, index, end);
  3552. done_index = index;
  3553. while (!done && !nr_to_write_done && (index <= end) &&
  3554. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3555. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3556. unsigned i;
  3557. scanned = 1;
  3558. for (i = 0; i < nr_pages; i++) {
  3559. struct page *page = pvec.pages[i];
  3560. done_index = page->index;
  3561. /*
  3562. * At this point we hold neither mapping->tree_lock nor
  3563. * lock on the page itself: the page may be truncated or
  3564. * invalidated (changing page->mapping to NULL), or even
  3565. * swizzled back from swapper_space to tmpfs file
  3566. * mapping
  3567. */
  3568. if (!trylock_page(page)) {
  3569. flush_fn(data);
  3570. lock_page(page);
  3571. }
  3572. if (unlikely(page->mapping != mapping)) {
  3573. unlock_page(page);
  3574. continue;
  3575. }
  3576. if (!wbc->range_cyclic && page->index > end) {
  3577. done = 1;
  3578. unlock_page(page);
  3579. continue;
  3580. }
  3581. if (wbc->sync_mode != WB_SYNC_NONE) {
  3582. if (PageWriteback(page))
  3583. flush_fn(data);
  3584. wait_on_page_writeback(page);
  3585. }
  3586. if (PageWriteback(page) ||
  3587. !clear_page_dirty_for_io(page)) {
  3588. unlock_page(page);
  3589. continue;
  3590. }
  3591. ret = (*writepage)(page, wbc, data);
  3592. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3593. unlock_page(page);
  3594. ret = 0;
  3595. }
  3596. if (ret < 0) {
  3597. /*
  3598. * done_index is set past this page,
  3599. * so media errors will not choke
  3600. * background writeout for the entire
  3601. * file. This has consequences for
  3602. * range_cyclic semantics (ie. it may
  3603. * not be suitable for data integrity
  3604. * writeout).
  3605. */
  3606. done_index = page->index + 1;
  3607. done = 1;
  3608. break;
  3609. }
  3610. /*
  3611. * the filesystem may choose to bump up nr_to_write.
  3612. * We have to make sure to honor the new nr_to_write
  3613. * at any time
  3614. */
  3615. nr_to_write_done = wbc->nr_to_write <= 0;
  3616. }
  3617. pagevec_release(&pvec);
  3618. cond_resched();
  3619. }
  3620. if (!scanned && !done) {
  3621. /*
  3622. * We hit the last page and there is more work to be done: wrap
  3623. * back to the start of the file
  3624. */
  3625. scanned = 1;
  3626. index = 0;
  3627. goto retry;
  3628. }
  3629. if (wbc->range_cyclic || (wbc->nr_to_write > 0 && range_whole))
  3630. mapping->writeback_index = done_index;
  3631. btrfs_add_delayed_iput(inode);
  3632. return ret;
  3633. }
  3634. static void flush_epd_write_bio(struct extent_page_data *epd)
  3635. {
  3636. if (epd->bio) {
  3637. int ret;
  3638. bio_set_op_attrs(epd->bio, REQ_OP_WRITE,
  3639. epd->sync_io ? REQ_SYNC : 0);
  3640. ret = submit_one_bio(epd->bio, 0, epd->bio_flags);
  3641. BUG_ON(ret < 0); /* -ENOMEM */
  3642. epd->bio = NULL;
  3643. }
  3644. }
  3645. static noinline void flush_write_bio(void *data)
  3646. {
  3647. struct extent_page_data *epd = data;
  3648. flush_epd_write_bio(epd);
  3649. }
  3650. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3651. get_extent_t *get_extent,
  3652. struct writeback_control *wbc)
  3653. {
  3654. int ret;
  3655. struct extent_page_data epd = {
  3656. .bio = NULL,
  3657. .tree = tree,
  3658. .get_extent = get_extent,
  3659. .extent_locked = 0,
  3660. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3661. .bio_flags = 0,
  3662. };
  3663. ret = __extent_writepage(page, wbc, &epd);
  3664. flush_epd_write_bio(&epd);
  3665. return ret;
  3666. }
  3667. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3668. u64 start, u64 end, get_extent_t *get_extent,
  3669. int mode)
  3670. {
  3671. int ret = 0;
  3672. struct address_space *mapping = inode->i_mapping;
  3673. struct page *page;
  3674. unsigned long nr_pages = (end - start + PAGE_SIZE) >>
  3675. PAGE_SHIFT;
  3676. struct extent_page_data epd = {
  3677. .bio = NULL,
  3678. .tree = tree,
  3679. .get_extent = get_extent,
  3680. .extent_locked = 1,
  3681. .sync_io = mode == WB_SYNC_ALL,
  3682. .bio_flags = 0,
  3683. };
  3684. struct writeback_control wbc_writepages = {
  3685. .sync_mode = mode,
  3686. .nr_to_write = nr_pages * 2,
  3687. .range_start = start,
  3688. .range_end = end + 1,
  3689. };
  3690. while (start <= end) {
  3691. page = find_get_page(mapping, start >> PAGE_SHIFT);
  3692. if (clear_page_dirty_for_io(page))
  3693. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3694. else {
  3695. if (tree->ops && tree->ops->writepage_end_io_hook)
  3696. tree->ops->writepage_end_io_hook(page, start,
  3697. start + PAGE_SIZE - 1,
  3698. NULL, 1);
  3699. unlock_page(page);
  3700. }
  3701. put_page(page);
  3702. start += PAGE_SIZE;
  3703. }
  3704. flush_epd_write_bio(&epd);
  3705. return ret;
  3706. }
  3707. int extent_writepages(struct extent_io_tree *tree,
  3708. struct address_space *mapping,
  3709. get_extent_t *get_extent,
  3710. struct writeback_control *wbc)
  3711. {
  3712. int ret = 0;
  3713. struct extent_page_data epd = {
  3714. .bio = NULL,
  3715. .tree = tree,
  3716. .get_extent = get_extent,
  3717. .extent_locked = 0,
  3718. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3719. .bio_flags = 0,
  3720. };
  3721. ret = extent_write_cache_pages(mapping, wbc, __extent_writepage, &epd,
  3722. flush_write_bio);
  3723. flush_epd_write_bio(&epd);
  3724. return ret;
  3725. }
  3726. int extent_readpages(struct extent_io_tree *tree,
  3727. struct address_space *mapping,
  3728. struct list_head *pages, unsigned nr_pages,
  3729. get_extent_t get_extent)
  3730. {
  3731. struct bio *bio = NULL;
  3732. unsigned page_idx;
  3733. unsigned long bio_flags = 0;
  3734. struct page *pagepool[16];
  3735. struct page *page;
  3736. struct extent_map *em_cached = NULL;
  3737. int nr = 0;
  3738. u64 prev_em_start = (u64)-1;
  3739. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3740. page = list_entry(pages->prev, struct page, lru);
  3741. prefetchw(&page->flags);
  3742. list_del(&page->lru);
  3743. if (add_to_page_cache_lru(page, mapping,
  3744. page->index,
  3745. readahead_gfp_mask(mapping))) {
  3746. put_page(page);
  3747. continue;
  3748. }
  3749. pagepool[nr++] = page;
  3750. if (nr < ARRAY_SIZE(pagepool))
  3751. continue;
  3752. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3753. &bio, 0, &bio_flags, &prev_em_start);
  3754. nr = 0;
  3755. }
  3756. if (nr)
  3757. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3758. &bio, 0, &bio_flags, &prev_em_start);
  3759. if (em_cached)
  3760. free_extent_map(em_cached);
  3761. BUG_ON(!list_empty(pages));
  3762. if (bio)
  3763. return submit_one_bio(bio, 0, bio_flags);
  3764. return 0;
  3765. }
  3766. /*
  3767. * basic invalidatepage code, this waits on any locked or writeback
  3768. * ranges corresponding to the page, and then deletes any extent state
  3769. * records from the tree
  3770. */
  3771. int extent_invalidatepage(struct extent_io_tree *tree,
  3772. struct page *page, unsigned long offset)
  3773. {
  3774. struct extent_state *cached_state = NULL;
  3775. u64 start = page_offset(page);
  3776. u64 end = start + PAGE_SIZE - 1;
  3777. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3778. start += ALIGN(offset, blocksize);
  3779. if (start > end)
  3780. return 0;
  3781. lock_extent_bits(tree, start, end, &cached_state);
  3782. wait_on_page_writeback(page);
  3783. clear_extent_bit(tree, start, end,
  3784. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3785. EXTENT_DO_ACCOUNTING,
  3786. 1, 1, &cached_state, GFP_NOFS);
  3787. return 0;
  3788. }
  3789. /*
  3790. * a helper for releasepage, this tests for areas of the page that
  3791. * are locked or under IO and drops the related state bits if it is safe
  3792. * to drop the page.
  3793. */
  3794. static int try_release_extent_state(struct extent_map_tree *map,
  3795. struct extent_io_tree *tree,
  3796. struct page *page, gfp_t mask)
  3797. {
  3798. u64 start = page_offset(page);
  3799. u64 end = start + PAGE_SIZE - 1;
  3800. int ret = 1;
  3801. if (test_range_bit(tree, start, end,
  3802. EXTENT_IOBITS, 0, NULL))
  3803. ret = 0;
  3804. else {
  3805. /*
  3806. * at this point we can safely clear everything except the
  3807. * locked bit and the nodatasum bit
  3808. */
  3809. ret = clear_extent_bit(tree, start, end,
  3810. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3811. 0, 0, NULL, mask);
  3812. /* if clear_extent_bit failed for enomem reasons,
  3813. * we can't allow the release to continue.
  3814. */
  3815. if (ret < 0)
  3816. ret = 0;
  3817. else
  3818. ret = 1;
  3819. }
  3820. return ret;
  3821. }
  3822. /*
  3823. * a helper for releasepage. As long as there are no locked extents
  3824. * in the range corresponding to the page, both state records and extent
  3825. * map records are removed
  3826. */
  3827. int try_release_extent_mapping(struct extent_map_tree *map,
  3828. struct extent_io_tree *tree, struct page *page,
  3829. gfp_t mask)
  3830. {
  3831. struct extent_map *em;
  3832. u64 start = page_offset(page);
  3833. u64 end = start + PAGE_SIZE - 1;
  3834. if (gfpflags_allow_blocking(mask) &&
  3835. page->mapping->host->i_size > SZ_16M) {
  3836. u64 len;
  3837. while (start <= end) {
  3838. len = end - start + 1;
  3839. write_lock(&map->lock);
  3840. em = lookup_extent_mapping(map, start, len);
  3841. if (!em) {
  3842. write_unlock(&map->lock);
  3843. break;
  3844. }
  3845. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3846. em->start != start) {
  3847. write_unlock(&map->lock);
  3848. free_extent_map(em);
  3849. break;
  3850. }
  3851. if (!test_range_bit(tree, em->start,
  3852. extent_map_end(em) - 1,
  3853. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3854. 0, NULL)) {
  3855. remove_extent_mapping(map, em);
  3856. /* once for the rb tree */
  3857. free_extent_map(em);
  3858. }
  3859. start = extent_map_end(em);
  3860. write_unlock(&map->lock);
  3861. /* once for us */
  3862. free_extent_map(em);
  3863. }
  3864. }
  3865. return try_release_extent_state(map, tree, page, mask);
  3866. }
  3867. /*
  3868. * helper function for fiemap, which doesn't want to see any holes.
  3869. * This maps until we find something past 'last'
  3870. */
  3871. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3872. u64 offset,
  3873. u64 last,
  3874. get_extent_t *get_extent)
  3875. {
  3876. u64 sectorsize = btrfs_inode_sectorsize(inode);
  3877. struct extent_map *em;
  3878. u64 len;
  3879. if (offset >= last)
  3880. return NULL;
  3881. while (1) {
  3882. len = last - offset;
  3883. if (len == 0)
  3884. break;
  3885. len = ALIGN(len, sectorsize);
  3886. em = get_extent(BTRFS_I(inode), NULL, 0, offset, len, 0);
  3887. if (IS_ERR_OR_NULL(em))
  3888. return em;
  3889. /* if this isn't a hole return it */
  3890. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3891. em->block_start != EXTENT_MAP_HOLE) {
  3892. return em;
  3893. }
  3894. /* this is a hole, advance to the next extent */
  3895. offset = extent_map_end(em);
  3896. free_extent_map(em);
  3897. if (offset >= last)
  3898. break;
  3899. }
  3900. return NULL;
  3901. }
  3902. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3903. __u64 start, __u64 len, get_extent_t *get_extent)
  3904. {
  3905. int ret = 0;
  3906. u64 off = start;
  3907. u64 max = start + len;
  3908. u32 flags = 0;
  3909. u32 found_type;
  3910. u64 last;
  3911. u64 last_for_get_extent = 0;
  3912. u64 disko = 0;
  3913. u64 isize = i_size_read(inode);
  3914. struct btrfs_key found_key;
  3915. struct extent_map *em = NULL;
  3916. struct extent_state *cached_state = NULL;
  3917. struct btrfs_path *path;
  3918. struct btrfs_root *root = BTRFS_I(inode)->root;
  3919. int end = 0;
  3920. u64 em_start = 0;
  3921. u64 em_len = 0;
  3922. u64 em_end = 0;
  3923. if (len == 0)
  3924. return -EINVAL;
  3925. path = btrfs_alloc_path();
  3926. if (!path)
  3927. return -ENOMEM;
  3928. path->leave_spinning = 1;
  3929. start = round_down(start, btrfs_inode_sectorsize(inode));
  3930. len = round_up(max, btrfs_inode_sectorsize(inode)) - start;
  3931. /*
  3932. * lookup the last file extent. We're not using i_size here
  3933. * because there might be preallocation past i_size
  3934. */
  3935. ret = btrfs_lookup_file_extent(NULL, root, path,
  3936. btrfs_ino(BTRFS_I(inode)), -1, 0);
  3937. if (ret < 0) {
  3938. btrfs_free_path(path);
  3939. return ret;
  3940. } else {
  3941. WARN_ON(!ret);
  3942. if (ret == 1)
  3943. ret = 0;
  3944. }
  3945. path->slots[0]--;
  3946. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3947. found_type = found_key.type;
  3948. /* No extents, but there might be delalloc bits */
  3949. if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
  3950. found_type != BTRFS_EXTENT_DATA_KEY) {
  3951. /* have to trust i_size as the end */
  3952. last = (u64)-1;
  3953. last_for_get_extent = isize;
  3954. } else {
  3955. /*
  3956. * remember the start of the last extent. There are a
  3957. * bunch of different factors that go into the length of the
  3958. * extent, so its much less complex to remember where it started
  3959. */
  3960. last = found_key.offset;
  3961. last_for_get_extent = last + 1;
  3962. }
  3963. btrfs_release_path(path);
  3964. /*
  3965. * we might have some extents allocated but more delalloc past those
  3966. * extents. so, we trust isize unless the start of the last extent is
  3967. * beyond isize
  3968. */
  3969. if (last < isize) {
  3970. last = (u64)-1;
  3971. last_for_get_extent = isize;
  3972. }
  3973. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  3974. &cached_state);
  3975. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3976. get_extent);
  3977. if (!em)
  3978. goto out;
  3979. if (IS_ERR(em)) {
  3980. ret = PTR_ERR(em);
  3981. goto out;
  3982. }
  3983. while (!end) {
  3984. u64 offset_in_extent = 0;
  3985. /* break if the extent we found is outside the range */
  3986. if (em->start >= max || extent_map_end(em) < off)
  3987. break;
  3988. /*
  3989. * get_extent may return an extent that starts before our
  3990. * requested range. We have to make sure the ranges
  3991. * we return to fiemap always move forward and don't
  3992. * overlap, so adjust the offsets here
  3993. */
  3994. em_start = max(em->start, off);
  3995. /*
  3996. * record the offset from the start of the extent
  3997. * for adjusting the disk offset below. Only do this if the
  3998. * extent isn't compressed since our in ram offset may be past
  3999. * what we have actually allocated on disk.
  4000. */
  4001. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4002. offset_in_extent = em_start - em->start;
  4003. em_end = extent_map_end(em);
  4004. em_len = em_end - em_start;
  4005. disko = 0;
  4006. flags = 0;
  4007. /*
  4008. * bump off for our next call to get_extent
  4009. */
  4010. off = extent_map_end(em);
  4011. if (off >= max)
  4012. end = 1;
  4013. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  4014. end = 1;
  4015. flags |= FIEMAP_EXTENT_LAST;
  4016. } else if (em->block_start == EXTENT_MAP_INLINE) {
  4017. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  4018. FIEMAP_EXTENT_NOT_ALIGNED);
  4019. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  4020. flags |= (FIEMAP_EXTENT_DELALLOC |
  4021. FIEMAP_EXTENT_UNKNOWN);
  4022. } else if (fieinfo->fi_extents_max) {
  4023. struct btrfs_trans_handle *trans;
  4024. u64 bytenr = em->block_start -
  4025. (em->start - em->orig_start);
  4026. disko = em->block_start + offset_in_extent;
  4027. /*
  4028. * We need a trans handle to get delayed refs
  4029. */
  4030. trans = btrfs_join_transaction(root);
  4031. /*
  4032. * It's OK if we can't start a trans we can still check
  4033. * from commit_root
  4034. */
  4035. if (IS_ERR(trans))
  4036. trans = NULL;
  4037. /*
  4038. * As btrfs supports shared space, this information
  4039. * can be exported to userspace tools via
  4040. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  4041. * then we're just getting a count and we can skip the
  4042. * lookup stuff.
  4043. */
  4044. ret = btrfs_check_shared(trans, root->fs_info,
  4045. root->objectid,
  4046. btrfs_ino(BTRFS_I(inode)), bytenr);
  4047. if (trans)
  4048. btrfs_end_transaction(trans);
  4049. if (ret < 0)
  4050. goto out_free;
  4051. if (ret)
  4052. flags |= FIEMAP_EXTENT_SHARED;
  4053. ret = 0;
  4054. }
  4055. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4056. flags |= FIEMAP_EXTENT_ENCODED;
  4057. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4058. flags |= FIEMAP_EXTENT_UNWRITTEN;
  4059. free_extent_map(em);
  4060. em = NULL;
  4061. if ((em_start >= last) || em_len == (u64)-1 ||
  4062. (last == (u64)-1 && isize <= em_end)) {
  4063. flags |= FIEMAP_EXTENT_LAST;
  4064. end = 1;
  4065. }
  4066. /* now scan forward to see if this is really the last extent. */
  4067. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4068. get_extent);
  4069. if (IS_ERR(em)) {
  4070. ret = PTR_ERR(em);
  4071. goto out;
  4072. }
  4073. if (!em) {
  4074. flags |= FIEMAP_EXTENT_LAST;
  4075. end = 1;
  4076. }
  4077. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  4078. em_len, flags);
  4079. if (ret) {
  4080. if (ret == 1)
  4081. ret = 0;
  4082. goto out_free;
  4083. }
  4084. }
  4085. out_free:
  4086. free_extent_map(em);
  4087. out:
  4088. btrfs_free_path(path);
  4089. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4090. &cached_state, GFP_NOFS);
  4091. return ret;
  4092. }
  4093. static void __free_extent_buffer(struct extent_buffer *eb)
  4094. {
  4095. btrfs_leak_debug_del(&eb->leak_list);
  4096. kmem_cache_free(extent_buffer_cache, eb);
  4097. }
  4098. int extent_buffer_under_io(struct extent_buffer *eb)
  4099. {
  4100. return (atomic_read(&eb->io_pages) ||
  4101. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4102. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4103. }
  4104. /*
  4105. * Helper for releasing extent buffer page.
  4106. */
  4107. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4108. {
  4109. unsigned long index;
  4110. struct page *page;
  4111. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4112. BUG_ON(extent_buffer_under_io(eb));
  4113. index = num_extent_pages(eb->start, eb->len);
  4114. if (index == 0)
  4115. return;
  4116. do {
  4117. index--;
  4118. page = eb->pages[index];
  4119. if (!page)
  4120. continue;
  4121. if (mapped)
  4122. spin_lock(&page->mapping->private_lock);
  4123. /*
  4124. * We do this since we'll remove the pages after we've
  4125. * removed the eb from the radix tree, so we could race
  4126. * and have this page now attached to the new eb. So
  4127. * only clear page_private if it's still connected to
  4128. * this eb.
  4129. */
  4130. if (PagePrivate(page) &&
  4131. page->private == (unsigned long)eb) {
  4132. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4133. BUG_ON(PageDirty(page));
  4134. BUG_ON(PageWriteback(page));
  4135. /*
  4136. * We need to make sure we haven't be attached
  4137. * to a new eb.
  4138. */
  4139. ClearPagePrivate(page);
  4140. set_page_private(page, 0);
  4141. /* One for the page private */
  4142. put_page(page);
  4143. }
  4144. if (mapped)
  4145. spin_unlock(&page->mapping->private_lock);
  4146. /* One for when we allocated the page */
  4147. put_page(page);
  4148. } while (index != 0);
  4149. }
  4150. /*
  4151. * Helper for releasing the extent buffer.
  4152. */
  4153. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4154. {
  4155. btrfs_release_extent_buffer_page(eb);
  4156. __free_extent_buffer(eb);
  4157. }
  4158. static struct extent_buffer *
  4159. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4160. unsigned long len)
  4161. {
  4162. struct extent_buffer *eb = NULL;
  4163. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS|__GFP_NOFAIL);
  4164. eb->start = start;
  4165. eb->len = len;
  4166. eb->fs_info = fs_info;
  4167. eb->bflags = 0;
  4168. rwlock_init(&eb->lock);
  4169. atomic_set(&eb->write_locks, 0);
  4170. atomic_set(&eb->read_locks, 0);
  4171. atomic_set(&eb->blocking_readers, 0);
  4172. atomic_set(&eb->blocking_writers, 0);
  4173. atomic_set(&eb->spinning_readers, 0);
  4174. atomic_set(&eb->spinning_writers, 0);
  4175. eb->lock_nested = 0;
  4176. init_waitqueue_head(&eb->write_lock_wq);
  4177. init_waitqueue_head(&eb->read_lock_wq);
  4178. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4179. spin_lock_init(&eb->refs_lock);
  4180. atomic_set(&eb->refs, 1);
  4181. atomic_set(&eb->io_pages, 0);
  4182. /*
  4183. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4184. */
  4185. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4186. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4187. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4188. return eb;
  4189. }
  4190. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4191. {
  4192. unsigned long i;
  4193. struct page *p;
  4194. struct extent_buffer *new;
  4195. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4196. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4197. if (new == NULL)
  4198. return NULL;
  4199. for (i = 0; i < num_pages; i++) {
  4200. p = alloc_page(GFP_NOFS);
  4201. if (!p) {
  4202. btrfs_release_extent_buffer(new);
  4203. return NULL;
  4204. }
  4205. attach_extent_buffer_page(new, p);
  4206. WARN_ON(PageDirty(p));
  4207. SetPageUptodate(p);
  4208. new->pages[i] = p;
  4209. copy_page(page_address(p), page_address(src->pages[i]));
  4210. }
  4211. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4212. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4213. return new;
  4214. }
  4215. struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4216. u64 start, unsigned long len)
  4217. {
  4218. struct extent_buffer *eb;
  4219. unsigned long num_pages;
  4220. unsigned long i;
  4221. num_pages = num_extent_pages(start, len);
  4222. eb = __alloc_extent_buffer(fs_info, start, len);
  4223. if (!eb)
  4224. return NULL;
  4225. for (i = 0; i < num_pages; i++) {
  4226. eb->pages[i] = alloc_page(GFP_NOFS);
  4227. if (!eb->pages[i])
  4228. goto err;
  4229. }
  4230. set_extent_buffer_uptodate(eb);
  4231. btrfs_set_header_nritems(eb, 0);
  4232. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4233. return eb;
  4234. err:
  4235. for (; i > 0; i--)
  4236. __free_page(eb->pages[i - 1]);
  4237. __free_extent_buffer(eb);
  4238. return NULL;
  4239. }
  4240. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4241. u64 start)
  4242. {
  4243. return __alloc_dummy_extent_buffer(fs_info, start, fs_info->nodesize);
  4244. }
  4245. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4246. {
  4247. int refs;
  4248. /* the ref bit is tricky. We have to make sure it is set
  4249. * if we have the buffer dirty. Otherwise the
  4250. * code to free a buffer can end up dropping a dirty
  4251. * page
  4252. *
  4253. * Once the ref bit is set, it won't go away while the
  4254. * buffer is dirty or in writeback, and it also won't
  4255. * go away while we have the reference count on the
  4256. * eb bumped.
  4257. *
  4258. * We can't just set the ref bit without bumping the
  4259. * ref on the eb because free_extent_buffer might
  4260. * see the ref bit and try to clear it. If this happens
  4261. * free_extent_buffer might end up dropping our original
  4262. * ref by mistake and freeing the page before we are able
  4263. * to add one more ref.
  4264. *
  4265. * So bump the ref count first, then set the bit. If someone
  4266. * beat us to it, drop the ref we added.
  4267. */
  4268. refs = atomic_read(&eb->refs);
  4269. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4270. return;
  4271. spin_lock(&eb->refs_lock);
  4272. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4273. atomic_inc(&eb->refs);
  4274. spin_unlock(&eb->refs_lock);
  4275. }
  4276. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4277. struct page *accessed)
  4278. {
  4279. unsigned long num_pages, i;
  4280. check_buffer_tree_ref(eb);
  4281. num_pages = num_extent_pages(eb->start, eb->len);
  4282. for (i = 0; i < num_pages; i++) {
  4283. struct page *p = eb->pages[i];
  4284. if (p != accessed)
  4285. mark_page_accessed(p);
  4286. }
  4287. }
  4288. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4289. u64 start)
  4290. {
  4291. struct extent_buffer *eb;
  4292. rcu_read_lock();
  4293. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4294. start >> PAGE_SHIFT);
  4295. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4296. rcu_read_unlock();
  4297. /*
  4298. * Lock our eb's refs_lock to avoid races with
  4299. * free_extent_buffer. When we get our eb it might be flagged
  4300. * with EXTENT_BUFFER_STALE and another task running
  4301. * free_extent_buffer might have seen that flag set,
  4302. * eb->refs == 2, that the buffer isn't under IO (dirty and
  4303. * writeback flags not set) and it's still in the tree (flag
  4304. * EXTENT_BUFFER_TREE_REF set), therefore being in the process
  4305. * of decrementing the extent buffer's reference count twice.
  4306. * So here we could race and increment the eb's reference count,
  4307. * clear its stale flag, mark it as dirty and drop our reference
  4308. * before the other task finishes executing free_extent_buffer,
  4309. * which would later result in an attempt to free an extent
  4310. * buffer that is dirty.
  4311. */
  4312. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  4313. spin_lock(&eb->refs_lock);
  4314. spin_unlock(&eb->refs_lock);
  4315. }
  4316. mark_extent_buffer_accessed(eb, NULL);
  4317. return eb;
  4318. }
  4319. rcu_read_unlock();
  4320. return NULL;
  4321. }
  4322. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4323. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4324. u64 start)
  4325. {
  4326. struct extent_buffer *eb, *exists = NULL;
  4327. int ret;
  4328. eb = find_extent_buffer(fs_info, start);
  4329. if (eb)
  4330. return eb;
  4331. eb = alloc_dummy_extent_buffer(fs_info, start);
  4332. if (!eb)
  4333. return NULL;
  4334. eb->fs_info = fs_info;
  4335. again:
  4336. ret = radix_tree_preload(GFP_NOFS);
  4337. if (ret)
  4338. goto free_eb;
  4339. spin_lock(&fs_info->buffer_lock);
  4340. ret = radix_tree_insert(&fs_info->buffer_radix,
  4341. start >> PAGE_SHIFT, eb);
  4342. spin_unlock(&fs_info->buffer_lock);
  4343. radix_tree_preload_end();
  4344. if (ret == -EEXIST) {
  4345. exists = find_extent_buffer(fs_info, start);
  4346. if (exists)
  4347. goto free_eb;
  4348. else
  4349. goto again;
  4350. }
  4351. check_buffer_tree_ref(eb);
  4352. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4353. /*
  4354. * We will free dummy extent buffer's if they come into
  4355. * free_extent_buffer with a ref count of 2, but if we are using this we
  4356. * want the buffers to stay in memory until we're done with them, so
  4357. * bump the ref count again.
  4358. */
  4359. atomic_inc(&eb->refs);
  4360. return eb;
  4361. free_eb:
  4362. btrfs_release_extent_buffer(eb);
  4363. return exists;
  4364. }
  4365. #endif
  4366. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4367. u64 start)
  4368. {
  4369. unsigned long len = fs_info->nodesize;
  4370. unsigned long num_pages = num_extent_pages(start, len);
  4371. unsigned long i;
  4372. unsigned long index = start >> PAGE_SHIFT;
  4373. struct extent_buffer *eb;
  4374. struct extent_buffer *exists = NULL;
  4375. struct page *p;
  4376. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4377. int uptodate = 1;
  4378. int ret;
  4379. if (!IS_ALIGNED(start, fs_info->sectorsize)) {
  4380. btrfs_err(fs_info, "bad tree block start %llu", start);
  4381. return ERR_PTR(-EINVAL);
  4382. }
  4383. eb = find_extent_buffer(fs_info, start);
  4384. if (eb)
  4385. return eb;
  4386. eb = __alloc_extent_buffer(fs_info, start, len);
  4387. if (!eb)
  4388. return ERR_PTR(-ENOMEM);
  4389. for (i = 0; i < num_pages; i++, index++) {
  4390. p = find_or_create_page(mapping, index, GFP_NOFS|__GFP_NOFAIL);
  4391. if (!p) {
  4392. exists = ERR_PTR(-ENOMEM);
  4393. goto free_eb;
  4394. }
  4395. spin_lock(&mapping->private_lock);
  4396. if (PagePrivate(p)) {
  4397. /*
  4398. * We could have already allocated an eb for this page
  4399. * and attached one so lets see if we can get a ref on
  4400. * the existing eb, and if we can we know it's good and
  4401. * we can just return that one, else we know we can just
  4402. * overwrite page->private.
  4403. */
  4404. exists = (struct extent_buffer *)p->private;
  4405. if (atomic_inc_not_zero(&exists->refs)) {
  4406. spin_unlock(&mapping->private_lock);
  4407. unlock_page(p);
  4408. put_page(p);
  4409. mark_extent_buffer_accessed(exists, p);
  4410. goto free_eb;
  4411. }
  4412. exists = NULL;
  4413. /*
  4414. * Do this so attach doesn't complain and we need to
  4415. * drop the ref the old guy had.
  4416. */
  4417. ClearPagePrivate(p);
  4418. WARN_ON(PageDirty(p));
  4419. put_page(p);
  4420. }
  4421. attach_extent_buffer_page(eb, p);
  4422. spin_unlock(&mapping->private_lock);
  4423. WARN_ON(PageDirty(p));
  4424. eb->pages[i] = p;
  4425. if (!PageUptodate(p))
  4426. uptodate = 0;
  4427. /*
  4428. * see below about how we avoid a nasty race with release page
  4429. * and why we unlock later
  4430. */
  4431. }
  4432. if (uptodate)
  4433. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4434. again:
  4435. ret = radix_tree_preload(GFP_NOFS);
  4436. if (ret) {
  4437. exists = ERR_PTR(ret);
  4438. goto free_eb;
  4439. }
  4440. spin_lock(&fs_info->buffer_lock);
  4441. ret = radix_tree_insert(&fs_info->buffer_radix,
  4442. start >> PAGE_SHIFT, eb);
  4443. spin_unlock(&fs_info->buffer_lock);
  4444. radix_tree_preload_end();
  4445. if (ret == -EEXIST) {
  4446. exists = find_extent_buffer(fs_info, start);
  4447. if (exists)
  4448. goto free_eb;
  4449. else
  4450. goto again;
  4451. }
  4452. /* add one reference for the tree */
  4453. check_buffer_tree_ref(eb);
  4454. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4455. /*
  4456. * there is a race where release page may have
  4457. * tried to find this extent buffer in the radix
  4458. * but failed. It will tell the VM it is safe to
  4459. * reclaim the, and it will clear the page private bit.
  4460. * We must make sure to set the page private bit properly
  4461. * after the extent buffer is in the radix tree so
  4462. * it doesn't get lost
  4463. */
  4464. SetPageChecked(eb->pages[0]);
  4465. for (i = 1; i < num_pages; i++) {
  4466. p = eb->pages[i];
  4467. ClearPageChecked(p);
  4468. unlock_page(p);
  4469. }
  4470. unlock_page(eb->pages[0]);
  4471. return eb;
  4472. free_eb:
  4473. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4474. for (i = 0; i < num_pages; i++) {
  4475. if (eb->pages[i])
  4476. unlock_page(eb->pages[i]);
  4477. }
  4478. btrfs_release_extent_buffer(eb);
  4479. return exists;
  4480. }
  4481. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4482. {
  4483. struct extent_buffer *eb =
  4484. container_of(head, struct extent_buffer, rcu_head);
  4485. __free_extent_buffer(eb);
  4486. }
  4487. /* Expects to have eb->eb_lock already held */
  4488. static int release_extent_buffer(struct extent_buffer *eb)
  4489. {
  4490. WARN_ON(atomic_read(&eb->refs) == 0);
  4491. if (atomic_dec_and_test(&eb->refs)) {
  4492. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4493. struct btrfs_fs_info *fs_info = eb->fs_info;
  4494. spin_unlock(&eb->refs_lock);
  4495. spin_lock(&fs_info->buffer_lock);
  4496. radix_tree_delete(&fs_info->buffer_radix,
  4497. eb->start >> PAGE_SHIFT);
  4498. spin_unlock(&fs_info->buffer_lock);
  4499. } else {
  4500. spin_unlock(&eb->refs_lock);
  4501. }
  4502. /* Should be safe to release our pages at this point */
  4503. btrfs_release_extent_buffer_page(eb);
  4504. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4505. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4506. __free_extent_buffer(eb);
  4507. return 1;
  4508. }
  4509. #endif
  4510. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4511. return 1;
  4512. }
  4513. spin_unlock(&eb->refs_lock);
  4514. return 0;
  4515. }
  4516. void free_extent_buffer(struct extent_buffer *eb)
  4517. {
  4518. int refs;
  4519. int old;
  4520. if (!eb)
  4521. return;
  4522. while (1) {
  4523. refs = atomic_read(&eb->refs);
  4524. if (refs <= 3)
  4525. break;
  4526. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4527. if (old == refs)
  4528. return;
  4529. }
  4530. spin_lock(&eb->refs_lock);
  4531. if (atomic_read(&eb->refs) == 2 &&
  4532. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4533. atomic_dec(&eb->refs);
  4534. if (atomic_read(&eb->refs) == 2 &&
  4535. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4536. !extent_buffer_under_io(eb) &&
  4537. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4538. atomic_dec(&eb->refs);
  4539. /*
  4540. * I know this is terrible, but it's temporary until we stop tracking
  4541. * the uptodate bits and such for the extent buffers.
  4542. */
  4543. release_extent_buffer(eb);
  4544. }
  4545. void free_extent_buffer_stale(struct extent_buffer *eb)
  4546. {
  4547. if (!eb)
  4548. return;
  4549. spin_lock(&eb->refs_lock);
  4550. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4551. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4552. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4553. atomic_dec(&eb->refs);
  4554. release_extent_buffer(eb);
  4555. }
  4556. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4557. {
  4558. unsigned long i;
  4559. unsigned long num_pages;
  4560. struct page *page;
  4561. num_pages = num_extent_pages(eb->start, eb->len);
  4562. for (i = 0; i < num_pages; i++) {
  4563. page = eb->pages[i];
  4564. if (!PageDirty(page))
  4565. continue;
  4566. lock_page(page);
  4567. WARN_ON(!PagePrivate(page));
  4568. clear_page_dirty_for_io(page);
  4569. spin_lock_irq(&page->mapping->tree_lock);
  4570. if (!PageDirty(page)) {
  4571. radix_tree_tag_clear(&page->mapping->page_tree,
  4572. page_index(page),
  4573. PAGECACHE_TAG_DIRTY);
  4574. }
  4575. spin_unlock_irq(&page->mapping->tree_lock);
  4576. ClearPageError(page);
  4577. unlock_page(page);
  4578. }
  4579. WARN_ON(atomic_read(&eb->refs) == 0);
  4580. }
  4581. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4582. {
  4583. unsigned long i;
  4584. unsigned long num_pages;
  4585. int was_dirty = 0;
  4586. check_buffer_tree_ref(eb);
  4587. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4588. num_pages = num_extent_pages(eb->start, eb->len);
  4589. WARN_ON(atomic_read(&eb->refs) == 0);
  4590. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4591. for (i = 0; i < num_pages; i++)
  4592. set_page_dirty(eb->pages[i]);
  4593. return was_dirty;
  4594. }
  4595. void clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4596. {
  4597. unsigned long i;
  4598. struct page *page;
  4599. unsigned long num_pages;
  4600. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4601. num_pages = num_extent_pages(eb->start, eb->len);
  4602. for (i = 0; i < num_pages; i++) {
  4603. page = eb->pages[i];
  4604. if (page)
  4605. ClearPageUptodate(page);
  4606. }
  4607. }
  4608. void set_extent_buffer_uptodate(struct extent_buffer *eb)
  4609. {
  4610. unsigned long i;
  4611. struct page *page;
  4612. unsigned long num_pages;
  4613. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4614. num_pages = num_extent_pages(eb->start, eb->len);
  4615. for (i = 0; i < num_pages; i++) {
  4616. page = eb->pages[i];
  4617. SetPageUptodate(page);
  4618. }
  4619. }
  4620. int extent_buffer_uptodate(struct extent_buffer *eb)
  4621. {
  4622. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4623. }
  4624. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4625. struct extent_buffer *eb, int wait,
  4626. get_extent_t *get_extent, int mirror_num)
  4627. {
  4628. unsigned long i;
  4629. struct page *page;
  4630. int err;
  4631. int ret = 0;
  4632. int locked_pages = 0;
  4633. int all_uptodate = 1;
  4634. unsigned long num_pages;
  4635. unsigned long num_reads = 0;
  4636. struct bio *bio = NULL;
  4637. unsigned long bio_flags = 0;
  4638. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4639. return 0;
  4640. num_pages = num_extent_pages(eb->start, eb->len);
  4641. for (i = 0; i < num_pages; i++) {
  4642. page = eb->pages[i];
  4643. if (wait == WAIT_NONE) {
  4644. if (!trylock_page(page))
  4645. goto unlock_exit;
  4646. } else {
  4647. lock_page(page);
  4648. }
  4649. locked_pages++;
  4650. }
  4651. /*
  4652. * We need to firstly lock all pages to make sure that
  4653. * the uptodate bit of our pages won't be affected by
  4654. * clear_extent_buffer_uptodate().
  4655. */
  4656. for (i = 0; i < num_pages; i++) {
  4657. page = eb->pages[i];
  4658. if (!PageUptodate(page)) {
  4659. num_reads++;
  4660. all_uptodate = 0;
  4661. }
  4662. }
  4663. if (all_uptodate) {
  4664. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4665. goto unlock_exit;
  4666. }
  4667. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4668. eb->read_mirror = 0;
  4669. atomic_set(&eb->io_pages, num_reads);
  4670. for (i = 0; i < num_pages; i++) {
  4671. page = eb->pages[i];
  4672. if (!PageUptodate(page)) {
  4673. if (ret) {
  4674. atomic_dec(&eb->io_pages);
  4675. unlock_page(page);
  4676. continue;
  4677. }
  4678. ClearPageError(page);
  4679. err = __extent_read_full_page(tree, page,
  4680. get_extent, &bio,
  4681. mirror_num, &bio_flags,
  4682. REQ_META);
  4683. if (err) {
  4684. ret = err;
  4685. /*
  4686. * We use &bio in above __extent_read_full_page,
  4687. * so we ensure that if it returns error, the
  4688. * current page fails to add itself to bio and
  4689. * it's been unlocked.
  4690. *
  4691. * We must dec io_pages by ourselves.
  4692. */
  4693. atomic_dec(&eb->io_pages);
  4694. }
  4695. } else {
  4696. unlock_page(page);
  4697. }
  4698. }
  4699. if (bio) {
  4700. err = submit_one_bio(bio, mirror_num, bio_flags);
  4701. if (err)
  4702. return err;
  4703. }
  4704. if (ret || wait != WAIT_COMPLETE)
  4705. return ret;
  4706. for (i = 0; i < num_pages; i++) {
  4707. page = eb->pages[i];
  4708. wait_on_page_locked(page);
  4709. if (!PageUptodate(page))
  4710. ret = -EIO;
  4711. }
  4712. return ret;
  4713. unlock_exit:
  4714. while (locked_pages > 0) {
  4715. locked_pages--;
  4716. page = eb->pages[locked_pages];
  4717. unlock_page(page);
  4718. }
  4719. return ret;
  4720. }
  4721. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4722. unsigned long start,
  4723. unsigned long len)
  4724. {
  4725. size_t cur;
  4726. size_t offset;
  4727. struct page *page;
  4728. char *kaddr;
  4729. char *dst = (char *)dstv;
  4730. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4731. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4732. WARN_ON(start > eb->len);
  4733. WARN_ON(start + len > eb->start + eb->len);
  4734. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4735. while (len > 0) {
  4736. page = eb->pages[i];
  4737. cur = min(len, (PAGE_SIZE - offset));
  4738. kaddr = page_address(page);
  4739. memcpy(dst, kaddr + offset, cur);
  4740. dst += cur;
  4741. len -= cur;
  4742. offset = 0;
  4743. i++;
  4744. }
  4745. }
  4746. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
  4747. unsigned long start,
  4748. unsigned long len)
  4749. {
  4750. size_t cur;
  4751. size_t offset;
  4752. struct page *page;
  4753. char *kaddr;
  4754. char __user *dst = (char __user *)dstv;
  4755. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4756. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4757. int ret = 0;
  4758. WARN_ON(start > eb->len);
  4759. WARN_ON(start + len > eb->start + eb->len);
  4760. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4761. while (len > 0) {
  4762. page = eb->pages[i];
  4763. cur = min(len, (PAGE_SIZE - offset));
  4764. kaddr = page_address(page);
  4765. if (copy_to_user(dst, kaddr + offset, cur)) {
  4766. ret = -EFAULT;
  4767. break;
  4768. }
  4769. dst += cur;
  4770. len -= cur;
  4771. offset = 0;
  4772. i++;
  4773. }
  4774. return ret;
  4775. }
  4776. /*
  4777. * return 0 if the item is found within a page.
  4778. * return 1 if the item spans two pages.
  4779. * return -EINVAL otherwise.
  4780. */
  4781. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4782. unsigned long min_len, char **map,
  4783. unsigned long *map_start,
  4784. unsigned long *map_len)
  4785. {
  4786. size_t offset = start & (PAGE_SIZE - 1);
  4787. char *kaddr;
  4788. struct page *p;
  4789. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4790. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4791. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4792. PAGE_SHIFT;
  4793. if (i != end_i)
  4794. return 1;
  4795. if (i == 0) {
  4796. offset = start_offset;
  4797. *map_start = 0;
  4798. } else {
  4799. offset = 0;
  4800. *map_start = ((u64)i << PAGE_SHIFT) - start_offset;
  4801. }
  4802. if (start + min_len > eb->len) {
  4803. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, wanted %lu %lu\n",
  4804. eb->start, eb->len, start, min_len);
  4805. return -EINVAL;
  4806. }
  4807. p = eb->pages[i];
  4808. kaddr = page_address(p);
  4809. *map = kaddr + offset;
  4810. *map_len = PAGE_SIZE - offset;
  4811. return 0;
  4812. }
  4813. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4814. unsigned long start,
  4815. unsigned long len)
  4816. {
  4817. size_t cur;
  4818. size_t offset;
  4819. struct page *page;
  4820. char *kaddr;
  4821. char *ptr = (char *)ptrv;
  4822. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4823. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4824. int ret = 0;
  4825. WARN_ON(start > eb->len);
  4826. WARN_ON(start + len > eb->start + eb->len);
  4827. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4828. while (len > 0) {
  4829. page = eb->pages[i];
  4830. cur = min(len, (PAGE_SIZE - offset));
  4831. kaddr = page_address(page);
  4832. ret = memcmp(ptr, kaddr + offset, cur);
  4833. if (ret)
  4834. break;
  4835. ptr += cur;
  4836. len -= cur;
  4837. offset = 0;
  4838. i++;
  4839. }
  4840. return ret;
  4841. }
  4842. void write_extent_buffer_chunk_tree_uuid(struct extent_buffer *eb,
  4843. const void *srcv)
  4844. {
  4845. char *kaddr;
  4846. WARN_ON(!PageUptodate(eb->pages[0]));
  4847. kaddr = page_address(eb->pages[0]);
  4848. memcpy(kaddr + offsetof(struct btrfs_header, chunk_tree_uuid), srcv,
  4849. BTRFS_FSID_SIZE);
  4850. }
  4851. void write_extent_buffer_fsid(struct extent_buffer *eb, const void *srcv)
  4852. {
  4853. char *kaddr;
  4854. WARN_ON(!PageUptodate(eb->pages[0]));
  4855. kaddr = page_address(eb->pages[0]);
  4856. memcpy(kaddr + offsetof(struct btrfs_header, fsid), srcv,
  4857. BTRFS_FSID_SIZE);
  4858. }
  4859. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4860. unsigned long start, unsigned long len)
  4861. {
  4862. size_t cur;
  4863. size_t offset;
  4864. struct page *page;
  4865. char *kaddr;
  4866. char *src = (char *)srcv;
  4867. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4868. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4869. WARN_ON(start > eb->len);
  4870. WARN_ON(start + len > eb->start + eb->len);
  4871. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4872. while (len > 0) {
  4873. page = eb->pages[i];
  4874. WARN_ON(!PageUptodate(page));
  4875. cur = min(len, PAGE_SIZE - offset);
  4876. kaddr = page_address(page);
  4877. memcpy(kaddr + offset, src, cur);
  4878. src += cur;
  4879. len -= cur;
  4880. offset = 0;
  4881. i++;
  4882. }
  4883. }
  4884. void memzero_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4885. unsigned long len)
  4886. {
  4887. size_t cur;
  4888. size_t offset;
  4889. struct page *page;
  4890. char *kaddr;
  4891. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4892. unsigned long i = (start_offset + start) >> PAGE_SHIFT;
  4893. WARN_ON(start > eb->len);
  4894. WARN_ON(start + len > eb->start + eb->len);
  4895. offset = (start_offset + start) & (PAGE_SIZE - 1);
  4896. while (len > 0) {
  4897. page = eb->pages[i];
  4898. WARN_ON(!PageUptodate(page));
  4899. cur = min(len, PAGE_SIZE - offset);
  4900. kaddr = page_address(page);
  4901. memset(kaddr + offset, 0, cur);
  4902. len -= cur;
  4903. offset = 0;
  4904. i++;
  4905. }
  4906. }
  4907. void copy_extent_buffer_full(struct extent_buffer *dst,
  4908. struct extent_buffer *src)
  4909. {
  4910. int i;
  4911. unsigned num_pages;
  4912. ASSERT(dst->len == src->len);
  4913. num_pages = num_extent_pages(dst->start, dst->len);
  4914. for (i = 0; i < num_pages; i++)
  4915. copy_page(page_address(dst->pages[i]),
  4916. page_address(src->pages[i]));
  4917. }
  4918. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4919. unsigned long dst_offset, unsigned long src_offset,
  4920. unsigned long len)
  4921. {
  4922. u64 dst_len = dst->len;
  4923. size_t cur;
  4924. size_t offset;
  4925. struct page *page;
  4926. char *kaddr;
  4927. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  4928. unsigned long i = (start_offset + dst_offset) >> PAGE_SHIFT;
  4929. WARN_ON(src->len != dst_len);
  4930. offset = (start_offset + dst_offset) &
  4931. (PAGE_SIZE - 1);
  4932. while (len > 0) {
  4933. page = dst->pages[i];
  4934. WARN_ON(!PageUptodate(page));
  4935. cur = min(len, (unsigned long)(PAGE_SIZE - offset));
  4936. kaddr = page_address(page);
  4937. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4938. src_offset += cur;
  4939. len -= cur;
  4940. offset = 0;
  4941. i++;
  4942. }
  4943. }
  4944. void le_bitmap_set(u8 *map, unsigned int start, int len)
  4945. {
  4946. u8 *p = map + BIT_BYTE(start);
  4947. const unsigned int size = start + len;
  4948. int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  4949. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
  4950. while (len - bits_to_set >= 0) {
  4951. *p |= mask_to_set;
  4952. len -= bits_to_set;
  4953. bits_to_set = BITS_PER_BYTE;
  4954. mask_to_set = ~0;
  4955. p++;
  4956. }
  4957. if (len) {
  4958. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  4959. *p |= mask_to_set;
  4960. }
  4961. }
  4962. void le_bitmap_clear(u8 *map, unsigned int start, int len)
  4963. {
  4964. u8 *p = map + BIT_BYTE(start);
  4965. const unsigned int size = start + len;
  4966. int bits_to_clear = BITS_PER_BYTE - (start % BITS_PER_BYTE);
  4967. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(start);
  4968. while (len - bits_to_clear >= 0) {
  4969. *p &= ~mask_to_clear;
  4970. len -= bits_to_clear;
  4971. bits_to_clear = BITS_PER_BYTE;
  4972. mask_to_clear = ~0;
  4973. p++;
  4974. }
  4975. if (len) {
  4976. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  4977. *p &= ~mask_to_clear;
  4978. }
  4979. }
  4980. /*
  4981. * eb_bitmap_offset() - calculate the page and offset of the byte containing the
  4982. * given bit number
  4983. * @eb: the extent buffer
  4984. * @start: offset of the bitmap item in the extent buffer
  4985. * @nr: bit number
  4986. * @page_index: return index of the page in the extent buffer that contains the
  4987. * given bit number
  4988. * @page_offset: return offset into the page given by page_index
  4989. *
  4990. * This helper hides the ugliness of finding the byte in an extent buffer which
  4991. * contains a given bit.
  4992. */
  4993. static inline void eb_bitmap_offset(struct extent_buffer *eb,
  4994. unsigned long start, unsigned long nr,
  4995. unsigned long *page_index,
  4996. size_t *page_offset)
  4997. {
  4998. size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);
  4999. size_t byte_offset = BIT_BYTE(nr);
  5000. size_t offset;
  5001. /*
  5002. * The byte we want is the offset of the extent buffer + the offset of
  5003. * the bitmap item in the extent buffer + the offset of the byte in the
  5004. * bitmap item.
  5005. */
  5006. offset = start_offset + start + byte_offset;
  5007. *page_index = offset >> PAGE_SHIFT;
  5008. *page_offset = offset & (PAGE_SIZE - 1);
  5009. }
  5010. /**
  5011. * extent_buffer_test_bit - determine whether a bit in a bitmap item is set
  5012. * @eb: the extent buffer
  5013. * @start: offset of the bitmap item in the extent buffer
  5014. * @nr: bit number to test
  5015. */
  5016. int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
  5017. unsigned long nr)
  5018. {
  5019. u8 *kaddr;
  5020. struct page *page;
  5021. unsigned long i;
  5022. size_t offset;
  5023. eb_bitmap_offset(eb, start, nr, &i, &offset);
  5024. page = eb->pages[i];
  5025. WARN_ON(!PageUptodate(page));
  5026. kaddr = page_address(page);
  5027. return 1U & (kaddr[offset] >> (nr & (BITS_PER_BYTE - 1)));
  5028. }
  5029. /**
  5030. * extent_buffer_bitmap_set - set an area of a bitmap
  5031. * @eb: the extent buffer
  5032. * @start: offset of the bitmap item in the extent buffer
  5033. * @pos: bit number of the first bit
  5034. * @len: number of bits to set
  5035. */
  5036. void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
  5037. unsigned long pos, unsigned long len)
  5038. {
  5039. u8 *kaddr;
  5040. struct page *page;
  5041. unsigned long i;
  5042. size_t offset;
  5043. const unsigned int size = pos + len;
  5044. int bits_to_set = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5045. u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(pos);
  5046. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5047. page = eb->pages[i];
  5048. WARN_ON(!PageUptodate(page));
  5049. kaddr = page_address(page);
  5050. while (len >= bits_to_set) {
  5051. kaddr[offset] |= mask_to_set;
  5052. len -= bits_to_set;
  5053. bits_to_set = BITS_PER_BYTE;
  5054. mask_to_set = ~0;
  5055. if (++offset >= PAGE_SIZE && len > 0) {
  5056. offset = 0;
  5057. page = eb->pages[++i];
  5058. WARN_ON(!PageUptodate(page));
  5059. kaddr = page_address(page);
  5060. }
  5061. }
  5062. if (len) {
  5063. mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
  5064. kaddr[offset] |= mask_to_set;
  5065. }
  5066. }
  5067. /**
  5068. * extent_buffer_bitmap_clear - clear an area of a bitmap
  5069. * @eb: the extent buffer
  5070. * @start: offset of the bitmap item in the extent buffer
  5071. * @pos: bit number of the first bit
  5072. * @len: number of bits to clear
  5073. */
  5074. void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
  5075. unsigned long pos, unsigned long len)
  5076. {
  5077. u8 *kaddr;
  5078. struct page *page;
  5079. unsigned long i;
  5080. size_t offset;
  5081. const unsigned int size = pos + len;
  5082. int bits_to_clear = BITS_PER_BYTE - (pos % BITS_PER_BYTE);
  5083. u8 mask_to_clear = BITMAP_FIRST_BYTE_MASK(pos);
  5084. eb_bitmap_offset(eb, start, pos, &i, &offset);
  5085. page = eb->pages[i];
  5086. WARN_ON(!PageUptodate(page));
  5087. kaddr = page_address(page);
  5088. while (len >= bits_to_clear) {
  5089. kaddr[offset] &= ~mask_to_clear;
  5090. len -= bits_to_clear;
  5091. bits_to_clear = BITS_PER_BYTE;
  5092. mask_to_clear = ~0;
  5093. if (++offset >= PAGE_SIZE && len > 0) {
  5094. offset = 0;
  5095. page = eb->pages[++i];
  5096. WARN_ON(!PageUptodate(page));
  5097. kaddr = page_address(page);
  5098. }
  5099. }
  5100. if (len) {
  5101. mask_to_clear &= BITMAP_LAST_BYTE_MASK(size);
  5102. kaddr[offset] &= ~mask_to_clear;
  5103. }
  5104. }
  5105. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  5106. {
  5107. unsigned long distance = (src > dst) ? src - dst : dst - src;
  5108. return distance < len;
  5109. }
  5110. static void copy_pages(struct page *dst_page, struct page *src_page,
  5111. unsigned long dst_off, unsigned long src_off,
  5112. unsigned long len)
  5113. {
  5114. char *dst_kaddr = page_address(dst_page);
  5115. char *src_kaddr;
  5116. int must_memmove = 0;
  5117. if (dst_page != src_page) {
  5118. src_kaddr = page_address(src_page);
  5119. } else {
  5120. src_kaddr = dst_kaddr;
  5121. if (areas_overlap(src_off, dst_off, len))
  5122. must_memmove = 1;
  5123. }
  5124. if (must_memmove)
  5125. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5126. else
  5127. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  5128. }
  5129. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5130. unsigned long src_offset, unsigned long len)
  5131. {
  5132. struct btrfs_fs_info *fs_info = dst->fs_info;
  5133. size_t cur;
  5134. size_t dst_off_in_page;
  5135. size_t src_off_in_page;
  5136. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5137. unsigned long dst_i;
  5138. unsigned long src_i;
  5139. if (src_offset + len > dst->len) {
  5140. btrfs_err(fs_info,
  5141. "memmove bogus src_offset %lu move len %lu dst len %lu",
  5142. src_offset, len, dst->len);
  5143. BUG_ON(1);
  5144. }
  5145. if (dst_offset + len > dst->len) {
  5146. btrfs_err(fs_info,
  5147. "memmove bogus dst_offset %lu move len %lu dst len %lu",
  5148. dst_offset, len, dst->len);
  5149. BUG_ON(1);
  5150. }
  5151. while (len > 0) {
  5152. dst_off_in_page = (start_offset + dst_offset) &
  5153. (PAGE_SIZE - 1);
  5154. src_off_in_page = (start_offset + src_offset) &
  5155. (PAGE_SIZE - 1);
  5156. dst_i = (start_offset + dst_offset) >> PAGE_SHIFT;
  5157. src_i = (start_offset + src_offset) >> PAGE_SHIFT;
  5158. cur = min(len, (unsigned long)(PAGE_SIZE -
  5159. src_off_in_page));
  5160. cur = min_t(unsigned long, cur,
  5161. (unsigned long)(PAGE_SIZE - dst_off_in_page));
  5162. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5163. dst_off_in_page, src_off_in_page, cur);
  5164. src_offset += cur;
  5165. dst_offset += cur;
  5166. len -= cur;
  5167. }
  5168. }
  5169. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  5170. unsigned long src_offset, unsigned long len)
  5171. {
  5172. struct btrfs_fs_info *fs_info = dst->fs_info;
  5173. size_t cur;
  5174. size_t dst_off_in_page;
  5175. size_t src_off_in_page;
  5176. unsigned long dst_end = dst_offset + len - 1;
  5177. unsigned long src_end = src_offset + len - 1;
  5178. size_t start_offset = dst->start & ((u64)PAGE_SIZE - 1);
  5179. unsigned long dst_i;
  5180. unsigned long src_i;
  5181. if (src_offset + len > dst->len) {
  5182. btrfs_err(fs_info,
  5183. "memmove bogus src_offset %lu move len %lu len %lu",
  5184. src_offset, len, dst->len);
  5185. BUG_ON(1);
  5186. }
  5187. if (dst_offset + len > dst->len) {
  5188. btrfs_err(fs_info,
  5189. "memmove bogus dst_offset %lu move len %lu len %lu",
  5190. dst_offset, len, dst->len);
  5191. BUG_ON(1);
  5192. }
  5193. if (dst_offset < src_offset) {
  5194. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  5195. return;
  5196. }
  5197. while (len > 0) {
  5198. dst_i = (start_offset + dst_end) >> PAGE_SHIFT;
  5199. src_i = (start_offset + src_end) >> PAGE_SHIFT;
  5200. dst_off_in_page = (start_offset + dst_end) &
  5201. (PAGE_SIZE - 1);
  5202. src_off_in_page = (start_offset + src_end) &
  5203. (PAGE_SIZE - 1);
  5204. cur = min_t(unsigned long, len, src_off_in_page + 1);
  5205. cur = min(cur, dst_off_in_page + 1);
  5206. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  5207. dst_off_in_page - cur + 1,
  5208. src_off_in_page - cur + 1, cur);
  5209. dst_end -= cur;
  5210. src_end -= cur;
  5211. len -= cur;
  5212. }
  5213. }
  5214. int try_release_extent_buffer(struct page *page)
  5215. {
  5216. struct extent_buffer *eb;
  5217. /*
  5218. * We need to make sure nobody is attaching this page to an eb right
  5219. * now.
  5220. */
  5221. spin_lock(&page->mapping->private_lock);
  5222. if (!PagePrivate(page)) {
  5223. spin_unlock(&page->mapping->private_lock);
  5224. return 1;
  5225. }
  5226. eb = (struct extent_buffer *)page->private;
  5227. BUG_ON(!eb);
  5228. /*
  5229. * This is a little awful but should be ok, we need to make sure that
  5230. * the eb doesn't disappear out from under us while we're looking at
  5231. * this page.
  5232. */
  5233. spin_lock(&eb->refs_lock);
  5234. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  5235. spin_unlock(&eb->refs_lock);
  5236. spin_unlock(&page->mapping->private_lock);
  5237. return 0;
  5238. }
  5239. spin_unlock(&page->mapping->private_lock);
  5240. /*
  5241. * If tree ref isn't set then we know the ref on this eb is a real ref,
  5242. * so just return, this page will likely be freed soon anyway.
  5243. */
  5244. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  5245. spin_unlock(&eb->refs_lock);
  5246. return 0;
  5247. }
  5248. return release_extent_buffer(eb);
  5249. }