extent_io.c 147 KB

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