extent_io.c 146 KB

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