extent_io.c 143 KB

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