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