extent_io.c 147 KB

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