extent_io.c 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638
  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, int err)
  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, err, 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, int err)
  2262. {
  2263. struct bio_vec *bvec;
  2264. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  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. if (err)
  2277. uptodate = 0;
  2278. bio_for_each_segment_all(bvec, bio, i) {
  2279. struct page *page = bvec->bv_page;
  2280. struct inode *inode = page->mapping->host;
  2281. pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
  2282. "mirror=%u\n", (u64)bio->bi_iter.bi_sector, err,
  2283. io_bio->mirror_num);
  2284. tree = &BTRFS_I(inode)->io_tree;
  2285. /* We always issue full-page reads, but if some block
  2286. * in a page fails to read, blk_update_request() will
  2287. * advance bv_offset and adjust bv_len to compensate.
  2288. * Print a warning for nonzero offsets, and an error
  2289. * if they don't add up to a full page. */
  2290. if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) {
  2291. if (bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE)
  2292. btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info,
  2293. "partial page read in btrfs with offset %u and length %u",
  2294. bvec->bv_offset, bvec->bv_len);
  2295. else
  2296. btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info,
  2297. "incomplete page read in btrfs with offset %u and "
  2298. "length %u",
  2299. bvec->bv_offset, bvec->bv_len);
  2300. }
  2301. start = page_offset(page);
  2302. end = start + bvec->bv_offset + bvec->bv_len - 1;
  2303. len = bvec->bv_len;
  2304. mirror = io_bio->mirror_num;
  2305. if (likely(uptodate && tree->ops &&
  2306. tree->ops->readpage_end_io_hook)) {
  2307. ret = tree->ops->readpage_end_io_hook(io_bio, offset,
  2308. page, start, end,
  2309. mirror);
  2310. if (ret)
  2311. uptodate = 0;
  2312. else
  2313. clean_io_failure(inode, start, page, 0);
  2314. }
  2315. if (likely(uptodate))
  2316. goto readpage_ok;
  2317. if (tree->ops && tree->ops->readpage_io_failed_hook) {
  2318. ret = tree->ops->readpage_io_failed_hook(page, mirror);
  2319. if (!ret && !err &&
  2320. test_bit(BIO_UPTODATE, &bio->bi_flags))
  2321. uptodate = 1;
  2322. } else {
  2323. /*
  2324. * The generic bio_readpage_error handles errors the
  2325. * following way: If possible, new read requests are
  2326. * created and submitted and will end up in
  2327. * end_bio_extent_readpage as well (if we're lucky, not
  2328. * in the !uptodate case). In that case it returns 0 and
  2329. * we just go on with the next page in our bio. If it
  2330. * can't handle the error it will return -EIO and we
  2331. * remain responsible for that page.
  2332. */
  2333. ret = bio_readpage_error(bio, offset, page, start, end,
  2334. mirror);
  2335. if (ret == 0) {
  2336. uptodate =
  2337. test_bit(BIO_UPTODATE, &bio->bi_flags);
  2338. if (err)
  2339. uptodate = 0;
  2340. offset += len;
  2341. continue;
  2342. }
  2343. }
  2344. readpage_ok:
  2345. if (likely(uptodate)) {
  2346. loff_t i_size = i_size_read(inode);
  2347. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  2348. unsigned off;
  2349. /* Zero out the end if this page straddles i_size */
  2350. off = i_size & (PAGE_CACHE_SIZE-1);
  2351. if (page->index == end_index && off)
  2352. zero_user_segment(page, off, PAGE_CACHE_SIZE);
  2353. SetPageUptodate(page);
  2354. } else {
  2355. ClearPageUptodate(page);
  2356. SetPageError(page);
  2357. }
  2358. unlock_page(page);
  2359. offset += len;
  2360. if (unlikely(!uptodate)) {
  2361. if (extent_len) {
  2362. endio_readpage_release_extent(tree,
  2363. extent_start,
  2364. extent_len, 1);
  2365. extent_start = 0;
  2366. extent_len = 0;
  2367. }
  2368. endio_readpage_release_extent(tree, start,
  2369. end - start + 1, 0);
  2370. } else if (!extent_len) {
  2371. extent_start = start;
  2372. extent_len = end + 1 - start;
  2373. } else if (extent_start + extent_len == start) {
  2374. extent_len += end + 1 - start;
  2375. } else {
  2376. endio_readpage_release_extent(tree, extent_start,
  2377. extent_len, uptodate);
  2378. extent_start = start;
  2379. extent_len = end + 1 - start;
  2380. }
  2381. }
  2382. if (extent_len)
  2383. endio_readpage_release_extent(tree, extent_start, extent_len,
  2384. uptodate);
  2385. if (io_bio->end_io)
  2386. io_bio->end_io(io_bio, err);
  2387. bio_put(bio);
  2388. }
  2389. /*
  2390. * this allocates from the btrfs_bioset. We're returning a bio right now
  2391. * but you can call btrfs_io_bio for the appropriate container_of magic
  2392. */
  2393. struct bio *
  2394. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  2395. gfp_t gfp_flags)
  2396. {
  2397. struct btrfs_io_bio *btrfs_bio;
  2398. struct bio *bio;
  2399. bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
  2400. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  2401. while (!bio && (nr_vecs /= 2)) {
  2402. bio = bio_alloc_bioset(gfp_flags,
  2403. nr_vecs, btrfs_bioset);
  2404. }
  2405. }
  2406. if (bio) {
  2407. bio->bi_bdev = bdev;
  2408. bio->bi_iter.bi_sector = first_sector;
  2409. btrfs_bio = btrfs_io_bio(bio);
  2410. btrfs_bio->csum = NULL;
  2411. btrfs_bio->csum_allocated = NULL;
  2412. btrfs_bio->end_io = NULL;
  2413. }
  2414. return bio;
  2415. }
  2416. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
  2417. {
  2418. struct btrfs_io_bio *btrfs_bio;
  2419. struct bio *new;
  2420. new = bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
  2421. if (new) {
  2422. btrfs_bio = btrfs_io_bio(new);
  2423. btrfs_bio->csum = NULL;
  2424. btrfs_bio->csum_allocated = NULL;
  2425. btrfs_bio->end_io = NULL;
  2426. }
  2427. return new;
  2428. }
  2429. /* this also allocates from the btrfs_bioset */
  2430. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  2431. {
  2432. struct btrfs_io_bio *btrfs_bio;
  2433. struct bio *bio;
  2434. bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
  2435. if (bio) {
  2436. btrfs_bio = btrfs_io_bio(bio);
  2437. btrfs_bio->csum = NULL;
  2438. btrfs_bio->csum_allocated = NULL;
  2439. btrfs_bio->end_io = NULL;
  2440. }
  2441. return bio;
  2442. }
  2443. static int __must_check submit_one_bio(int rw, struct bio *bio,
  2444. int mirror_num, unsigned long bio_flags)
  2445. {
  2446. int ret = 0;
  2447. struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
  2448. struct page *page = bvec->bv_page;
  2449. struct extent_io_tree *tree = bio->bi_private;
  2450. u64 start;
  2451. start = page_offset(page) + bvec->bv_offset;
  2452. bio->bi_private = NULL;
  2453. bio_get(bio);
  2454. if (tree->ops && tree->ops->submit_bio_hook)
  2455. ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
  2456. mirror_num, bio_flags, start);
  2457. else
  2458. btrfsic_submit_bio(rw, bio);
  2459. bio_put(bio);
  2460. return ret;
  2461. }
  2462. static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
  2463. unsigned long offset, size_t size, struct bio *bio,
  2464. unsigned long bio_flags)
  2465. {
  2466. int ret = 0;
  2467. if (tree->ops && tree->ops->merge_bio_hook)
  2468. ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
  2469. bio_flags);
  2470. BUG_ON(ret < 0);
  2471. return ret;
  2472. }
  2473. static int submit_extent_page(int rw, struct extent_io_tree *tree,
  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. {
  2484. int ret = 0;
  2485. struct bio *bio;
  2486. int nr;
  2487. int contig = 0;
  2488. int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
  2489. int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
  2490. size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
  2491. if (bio_ret && *bio_ret) {
  2492. bio = *bio_ret;
  2493. if (old_compressed)
  2494. contig = bio->bi_iter.bi_sector == sector;
  2495. else
  2496. contig = bio_end_sector(bio) == sector;
  2497. if (prev_bio_flags != bio_flags || !contig ||
  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. return 0;
  2509. }
  2510. }
  2511. if (this_compressed)
  2512. nr = BIO_MAX_PAGES;
  2513. else
  2514. nr = bio_get_nr_vecs(bdev);
  2515. bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
  2516. if (!bio)
  2517. return -ENOMEM;
  2518. bio_add_page(bio, page, page_size, offset);
  2519. bio->bi_end_io = end_io_func;
  2520. bio->bi_private = tree;
  2521. if (bio_ret)
  2522. *bio_ret = bio;
  2523. else
  2524. ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
  2525. return ret;
  2526. }
  2527. static void attach_extent_buffer_page(struct extent_buffer *eb,
  2528. struct page *page)
  2529. {
  2530. if (!PagePrivate(page)) {
  2531. SetPagePrivate(page);
  2532. page_cache_get(page);
  2533. set_page_private(page, (unsigned long)eb);
  2534. } else {
  2535. WARN_ON(page->private != (unsigned long)eb);
  2536. }
  2537. }
  2538. void set_page_extent_mapped(struct page *page)
  2539. {
  2540. if (!PagePrivate(page)) {
  2541. SetPagePrivate(page);
  2542. page_cache_get(page);
  2543. set_page_private(page, EXTENT_PAGE_PRIVATE);
  2544. }
  2545. }
  2546. static struct extent_map *
  2547. __get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
  2548. u64 start, u64 len, get_extent_t *get_extent,
  2549. struct extent_map **em_cached)
  2550. {
  2551. struct extent_map *em;
  2552. if (em_cached && *em_cached) {
  2553. em = *em_cached;
  2554. if (extent_map_in_tree(em) && start >= em->start &&
  2555. start < extent_map_end(em)) {
  2556. atomic_inc(&em->refs);
  2557. return em;
  2558. }
  2559. free_extent_map(em);
  2560. *em_cached = NULL;
  2561. }
  2562. em = get_extent(inode, page, pg_offset, start, len, 0);
  2563. if (em_cached && !IS_ERR_OR_NULL(em)) {
  2564. BUG_ON(*em_cached);
  2565. atomic_inc(&em->refs);
  2566. *em_cached = em;
  2567. }
  2568. return em;
  2569. }
  2570. /*
  2571. * basic readpage implementation. Locked extent state structs are inserted
  2572. * into the tree that are removed when the IO is done (by the end_io
  2573. * handlers)
  2574. * XXX JDM: This needs looking at to ensure proper page locking
  2575. */
  2576. static int __do_readpage(struct extent_io_tree *tree,
  2577. struct page *page,
  2578. get_extent_t *get_extent,
  2579. struct extent_map **em_cached,
  2580. struct bio **bio, int mirror_num,
  2581. unsigned long *bio_flags, int rw)
  2582. {
  2583. struct inode *inode = page->mapping->host;
  2584. u64 start = page_offset(page);
  2585. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2586. u64 end;
  2587. u64 cur = start;
  2588. u64 extent_offset;
  2589. u64 last_byte = i_size_read(inode);
  2590. u64 block_start;
  2591. u64 cur_end;
  2592. sector_t sector;
  2593. struct extent_map *em;
  2594. struct block_device *bdev;
  2595. int ret;
  2596. int nr = 0;
  2597. int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
  2598. size_t pg_offset = 0;
  2599. size_t iosize;
  2600. size_t disk_io_size;
  2601. size_t blocksize = inode->i_sb->s_blocksize;
  2602. unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
  2603. set_page_extent_mapped(page);
  2604. end = page_end;
  2605. if (!PageUptodate(page)) {
  2606. if (cleancache_get_page(page) == 0) {
  2607. BUG_ON(blocksize != PAGE_SIZE);
  2608. unlock_extent(tree, start, end);
  2609. goto out;
  2610. }
  2611. }
  2612. if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
  2613. char *userpage;
  2614. size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
  2615. if (zero_offset) {
  2616. iosize = PAGE_CACHE_SIZE - zero_offset;
  2617. userpage = kmap_atomic(page);
  2618. memset(userpage + zero_offset, 0, iosize);
  2619. flush_dcache_page(page);
  2620. kunmap_atomic(userpage);
  2621. }
  2622. }
  2623. while (cur <= end) {
  2624. unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
  2625. if (cur >= last_byte) {
  2626. char *userpage;
  2627. struct extent_state *cached = NULL;
  2628. iosize = PAGE_CACHE_SIZE - pg_offset;
  2629. userpage = kmap_atomic(page);
  2630. memset(userpage + pg_offset, 0, iosize);
  2631. flush_dcache_page(page);
  2632. kunmap_atomic(userpage);
  2633. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2634. &cached, GFP_NOFS);
  2635. if (!parent_locked)
  2636. unlock_extent_cached(tree, cur,
  2637. cur + iosize - 1,
  2638. &cached, GFP_NOFS);
  2639. break;
  2640. }
  2641. em = __get_extent_map(inode, page, pg_offset, cur,
  2642. end - cur + 1, get_extent, em_cached);
  2643. if (IS_ERR_OR_NULL(em)) {
  2644. SetPageError(page);
  2645. if (!parent_locked)
  2646. unlock_extent(tree, cur, end);
  2647. break;
  2648. }
  2649. extent_offset = cur - em->start;
  2650. BUG_ON(extent_map_end(em) <= cur);
  2651. BUG_ON(end < cur);
  2652. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  2653. this_bio_flag |= EXTENT_BIO_COMPRESSED;
  2654. extent_set_compress_type(&this_bio_flag,
  2655. em->compress_type);
  2656. }
  2657. iosize = min(extent_map_end(em) - cur, end - cur + 1);
  2658. cur_end = min(extent_map_end(em) - 1, end);
  2659. iosize = ALIGN(iosize, blocksize);
  2660. if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
  2661. disk_io_size = em->block_len;
  2662. sector = em->block_start >> 9;
  2663. } else {
  2664. sector = (em->block_start + extent_offset) >> 9;
  2665. disk_io_size = iosize;
  2666. }
  2667. bdev = em->bdev;
  2668. block_start = em->block_start;
  2669. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  2670. block_start = EXTENT_MAP_HOLE;
  2671. free_extent_map(em);
  2672. em = NULL;
  2673. /* we've found a hole, just zero and go on */
  2674. if (block_start == EXTENT_MAP_HOLE) {
  2675. char *userpage;
  2676. struct extent_state *cached = NULL;
  2677. userpage = kmap_atomic(page);
  2678. memset(userpage + pg_offset, 0, iosize);
  2679. flush_dcache_page(page);
  2680. kunmap_atomic(userpage);
  2681. set_extent_uptodate(tree, cur, cur + iosize - 1,
  2682. &cached, GFP_NOFS);
  2683. unlock_extent_cached(tree, cur, cur + iosize - 1,
  2684. &cached, GFP_NOFS);
  2685. cur = cur + iosize;
  2686. pg_offset += iosize;
  2687. continue;
  2688. }
  2689. /* the get_extent function already copied into the page */
  2690. if (test_range_bit(tree, cur, cur_end,
  2691. EXTENT_UPTODATE, 1, NULL)) {
  2692. check_page_uptodate(tree, page);
  2693. if (!parent_locked)
  2694. unlock_extent(tree, cur, cur + iosize - 1);
  2695. cur = cur + iosize;
  2696. pg_offset += iosize;
  2697. continue;
  2698. }
  2699. /* we have an inline extent but it didn't get marked up
  2700. * to date. Error out
  2701. */
  2702. if (block_start == EXTENT_MAP_INLINE) {
  2703. SetPageError(page);
  2704. if (!parent_locked)
  2705. unlock_extent(tree, cur, cur + iosize - 1);
  2706. cur = cur + iosize;
  2707. pg_offset += iosize;
  2708. continue;
  2709. }
  2710. pnr -= page->index;
  2711. ret = submit_extent_page(rw, tree, page,
  2712. sector, disk_io_size, pg_offset,
  2713. bdev, bio, pnr,
  2714. end_bio_extent_readpage, mirror_num,
  2715. *bio_flags,
  2716. this_bio_flag);
  2717. if (!ret) {
  2718. nr++;
  2719. *bio_flags = this_bio_flag;
  2720. } else {
  2721. SetPageError(page);
  2722. if (!parent_locked)
  2723. unlock_extent(tree, cur, cur + iosize - 1);
  2724. }
  2725. cur = cur + iosize;
  2726. pg_offset += iosize;
  2727. }
  2728. out:
  2729. if (!nr) {
  2730. if (!PageError(page))
  2731. SetPageUptodate(page);
  2732. unlock_page(page);
  2733. }
  2734. return 0;
  2735. }
  2736. static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
  2737. struct page *pages[], int nr_pages,
  2738. u64 start, u64 end,
  2739. get_extent_t *get_extent,
  2740. struct extent_map **em_cached,
  2741. struct bio **bio, int mirror_num,
  2742. unsigned long *bio_flags, int rw)
  2743. {
  2744. struct inode *inode;
  2745. struct btrfs_ordered_extent *ordered;
  2746. int index;
  2747. inode = pages[0]->mapping->host;
  2748. while (1) {
  2749. lock_extent(tree, start, end);
  2750. ordered = btrfs_lookup_ordered_range(inode, start,
  2751. end - start + 1);
  2752. if (!ordered)
  2753. break;
  2754. unlock_extent(tree, start, end);
  2755. btrfs_start_ordered_extent(inode, ordered, 1);
  2756. btrfs_put_ordered_extent(ordered);
  2757. }
  2758. for (index = 0; index < nr_pages; index++) {
  2759. __do_readpage(tree, pages[index], get_extent, em_cached, bio,
  2760. mirror_num, bio_flags, rw);
  2761. page_cache_release(pages[index]);
  2762. }
  2763. }
  2764. static void __extent_readpages(struct extent_io_tree *tree,
  2765. struct page *pages[],
  2766. int nr_pages, get_extent_t *get_extent,
  2767. struct extent_map **em_cached,
  2768. struct bio **bio, int mirror_num,
  2769. unsigned long *bio_flags, int rw)
  2770. {
  2771. u64 start = 0;
  2772. u64 end = 0;
  2773. u64 page_start;
  2774. int index;
  2775. int first_index = 0;
  2776. for (index = 0; index < nr_pages; index++) {
  2777. page_start = page_offset(pages[index]);
  2778. if (!end) {
  2779. start = page_start;
  2780. end = start + PAGE_CACHE_SIZE - 1;
  2781. first_index = index;
  2782. } else if (end + 1 == page_start) {
  2783. end += PAGE_CACHE_SIZE;
  2784. } else {
  2785. __do_contiguous_readpages(tree, &pages[first_index],
  2786. index - first_index, start,
  2787. end, get_extent, em_cached,
  2788. bio, mirror_num, bio_flags,
  2789. rw);
  2790. start = page_start;
  2791. end = start + PAGE_CACHE_SIZE - 1;
  2792. first_index = index;
  2793. }
  2794. }
  2795. if (end)
  2796. __do_contiguous_readpages(tree, &pages[first_index],
  2797. index - first_index, start,
  2798. end, get_extent, em_cached, bio,
  2799. mirror_num, bio_flags, rw);
  2800. }
  2801. static int __extent_read_full_page(struct extent_io_tree *tree,
  2802. struct page *page,
  2803. get_extent_t *get_extent,
  2804. struct bio **bio, int mirror_num,
  2805. unsigned long *bio_flags, int rw)
  2806. {
  2807. struct inode *inode = page->mapping->host;
  2808. struct btrfs_ordered_extent *ordered;
  2809. u64 start = page_offset(page);
  2810. u64 end = start + PAGE_CACHE_SIZE - 1;
  2811. int ret;
  2812. while (1) {
  2813. lock_extent(tree, start, end);
  2814. ordered = btrfs_lookup_ordered_extent(inode, start);
  2815. if (!ordered)
  2816. break;
  2817. unlock_extent(tree, start, end);
  2818. btrfs_start_ordered_extent(inode, ordered, 1);
  2819. btrfs_put_ordered_extent(ordered);
  2820. }
  2821. ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
  2822. bio_flags, rw);
  2823. return ret;
  2824. }
  2825. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  2826. get_extent_t *get_extent, int mirror_num)
  2827. {
  2828. struct bio *bio = NULL;
  2829. unsigned long bio_flags = 0;
  2830. int ret;
  2831. ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
  2832. &bio_flags, READ);
  2833. if (bio)
  2834. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2835. return ret;
  2836. }
  2837. int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
  2838. get_extent_t *get_extent, int mirror_num)
  2839. {
  2840. struct bio *bio = NULL;
  2841. unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED;
  2842. int ret;
  2843. ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num,
  2844. &bio_flags, READ);
  2845. if (bio)
  2846. ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
  2847. return ret;
  2848. }
  2849. static noinline void update_nr_written(struct page *page,
  2850. struct writeback_control *wbc,
  2851. unsigned long nr_written)
  2852. {
  2853. wbc->nr_to_write -= nr_written;
  2854. if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
  2855. wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
  2856. page->mapping->writeback_index = page->index + nr_written;
  2857. }
  2858. /*
  2859. * helper for __extent_writepage, doing all of the delayed allocation setup.
  2860. *
  2861. * This returns 1 if our fill_delalloc function did all the work required
  2862. * to write the page (copy into inline extent). In this case the IO has
  2863. * been started and the page is already unlocked.
  2864. *
  2865. * This returns 0 if all went well (page still locked)
  2866. * This returns < 0 if there were errors (page still locked)
  2867. */
  2868. static noinline_for_stack int writepage_delalloc(struct inode *inode,
  2869. struct page *page, struct writeback_control *wbc,
  2870. struct extent_page_data *epd,
  2871. u64 delalloc_start,
  2872. unsigned long *nr_written)
  2873. {
  2874. struct extent_io_tree *tree = epd->tree;
  2875. u64 page_end = delalloc_start + PAGE_CACHE_SIZE - 1;
  2876. u64 nr_delalloc;
  2877. u64 delalloc_to_write = 0;
  2878. u64 delalloc_end = 0;
  2879. int ret;
  2880. int page_started = 0;
  2881. if (epd->extent_locked || !tree->ops || !tree->ops->fill_delalloc)
  2882. return 0;
  2883. while (delalloc_end < page_end) {
  2884. nr_delalloc = find_lock_delalloc_range(inode, tree,
  2885. page,
  2886. &delalloc_start,
  2887. &delalloc_end,
  2888. BTRFS_MAX_EXTENT_SIZE);
  2889. if (nr_delalloc == 0) {
  2890. delalloc_start = delalloc_end + 1;
  2891. continue;
  2892. }
  2893. ret = tree->ops->fill_delalloc(inode, page,
  2894. delalloc_start,
  2895. delalloc_end,
  2896. &page_started,
  2897. nr_written);
  2898. /* File system has been set read-only */
  2899. if (ret) {
  2900. SetPageError(page);
  2901. /* fill_delalloc should be return < 0 for error
  2902. * but just in case, we use > 0 here meaning the
  2903. * IO is started, so we don't want to return > 0
  2904. * unless things are going well.
  2905. */
  2906. ret = ret < 0 ? ret : -EIO;
  2907. goto done;
  2908. }
  2909. /*
  2910. * delalloc_end is already one less than the total
  2911. * length, so we don't subtract one from
  2912. * PAGE_CACHE_SIZE
  2913. */
  2914. delalloc_to_write += (delalloc_end - delalloc_start +
  2915. PAGE_CACHE_SIZE) >>
  2916. PAGE_CACHE_SHIFT;
  2917. delalloc_start = delalloc_end + 1;
  2918. }
  2919. if (wbc->nr_to_write < delalloc_to_write) {
  2920. int thresh = 8192;
  2921. if (delalloc_to_write < thresh * 2)
  2922. thresh = delalloc_to_write;
  2923. wbc->nr_to_write = min_t(u64, delalloc_to_write,
  2924. thresh);
  2925. }
  2926. /* did the fill delalloc function already unlock and start
  2927. * the IO?
  2928. */
  2929. if (page_started) {
  2930. /*
  2931. * we've unlocked the page, so we can't update
  2932. * the mapping's writeback index, just update
  2933. * nr_to_write.
  2934. */
  2935. wbc->nr_to_write -= *nr_written;
  2936. return 1;
  2937. }
  2938. ret = 0;
  2939. done:
  2940. return ret;
  2941. }
  2942. /*
  2943. * helper for __extent_writepage. This calls the writepage start hooks,
  2944. * and does the loop to map the page into extents and bios.
  2945. *
  2946. * We return 1 if the IO is started and the page is unlocked,
  2947. * 0 if all went well (page still locked)
  2948. * < 0 if there were errors (page still locked)
  2949. */
  2950. static noinline_for_stack int __extent_writepage_io(struct inode *inode,
  2951. struct page *page,
  2952. struct writeback_control *wbc,
  2953. struct extent_page_data *epd,
  2954. loff_t i_size,
  2955. unsigned long nr_written,
  2956. int write_flags, int *nr_ret)
  2957. {
  2958. struct extent_io_tree *tree = epd->tree;
  2959. u64 start = page_offset(page);
  2960. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  2961. u64 end;
  2962. u64 cur = start;
  2963. u64 extent_offset;
  2964. u64 block_start;
  2965. u64 iosize;
  2966. sector_t sector;
  2967. struct extent_state *cached_state = NULL;
  2968. struct extent_map *em;
  2969. struct block_device *bdev;
  2970. size_t pg_offset = 0;
  2971. size_t blocksize;
  2972. int ret = 0;
  2973. int nr = 0;
  2974. bool compressed;
  2975. if (tree->ops && tree->ops->writepage_start_hook) {
  2976. ret = tree->ops->writepage_start_hook(page, start,
  2977. page_end);
  2978. if (ret) {
  2979. /* Fixup worker will requeue */
  2980. if (ret == -EBUSY)
  2981. wbc->pages_skipped++;
  2982. else
  2983. redirty_page_for_writepage(wbc, page);
  2984. update_nr_written(page, wbc, nr_written);
  2985. unlock_page(page);
  2986. ret = 1;
  2987. goto done_unlocked;
  2988. }
  2989. }
  2990. /*
  2991. * we don't want to touch the inode after unlocking the page,
  2992. * so we update the mapping writeback index now
  2993. */
  2994. update_nr_written(page, wbc, nr_written + 1);
  2995. end = page_end;
  2996. if (i_size <= start) {
  2997. if (tree->ops && tree->ops->writepage_end_io_hook)
  2998. tree->ops->writepage_end_io_hook(page, start,
  2999. page_end, NULL, 1);
  3000. goto done;
  3001. }
  3002. blocksize = inode->i_sb->s_blocksize;
  3003. while (cur <= end) {
  3004. u64 em_end;
  3005. if (cur >= i_size) {
  3006. if (tree->ops && tree->ops->writepage_end_io_hook)
  3007. tree->ops->writepage_end_io_hook(page, cur,
  3008. page_end, NULL, 1);
  3009. break;
  3010. }
  3011. em = epd->get_extent(inode, page, pg_offset, cur,
  3012. end - cur + 1, 1);
  3013. if (IS_ERR_OR_NULL(em)) {
  3014. SetPageError(page);
  3015. ret = PTR_ERR_OR_ZERO(em);
  3016. break;
  3017. }
  3018. extent_offset = cur - em->start;
  3019. em_end = extent_map_end(em);
  3020. BUG_ON(em_end <= cur);
  3021. BUG_ON(end < cur);
  3022. iosize = min(em_end - cur, end - cur + 1);
  3023. iosize = ALIGN(iosize, blocksize);
  3024. sector = (em->block_start + extent_offset) >> 9;
  3025. bdev = em->bdev;
  3026. block_start = em->block_start;
  3027. compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3028. free_extent_map(em);
  3029. em = NULL;
  3030. /*
  3031. * compressed and inline extents are written through other
  3032. * paths in the FS
  3033. */
  3034. if (compressed || block_start == EXTENT_MAP_HOLE ||
  3035. block_start == EXTENT_MAP_INLINE) {
  3036. /*
  3037. * end_io notification does not happen here for
  3038. * compressed extents
  3039. */
  3040. if (!compressed && tree->ops &&
  3041. tree->ops->writepage_end_io_hook)
  3042. tree->ops->writepage_end_io_hook(page, cur,
  3043. cur + iosize - 1,
  3044. NULL, 1);
  3045. else if (compressed) {
  3046. /* we don't want to end_page_writeback on
  3047. * a compressed extent. this happens
  3048. * elsewhere
  3049. */
  3050. nr++;
  3051. }
  3052. cur += iosize;
  3053. pg_offset += iosize;
  3054. continue;
  3055. }
  3056. if (tree->ops && tree->ops->writepage_io_hook) {
  3057. ret = tree->ops->writepage_io_hook(page, cur,
  3058. cur + iosize - 1);
  3059. } else {
  3060. ret = 0;
  3061. }
  3062. if (ret) {
  3063. SetPageError(page);
  3064. } else {
  3065. unsigned long max_nr = (i_size >> PAGE_CACHE_SHIFT) + 1;
  3066. set_range_writeback(tree, cur, cur + iosize - 1);
  3067. if (!PageWriteback(page)) {
  3068. btrfs_err(BTRFS_I(inode)->root->fs_info,
  3069. "page %lu not writeback, cur %llu end %llu",
  3070. page->index, cur, end);
  3071. }
  3072. ret = submit_extent_page(write_flags, tree, page,
  3073. sector, iosize, pg_offset,
  3074. bdev, &epd->bio, max_nr,
  3075. end_bio_extent_writepage,
  3076. 0, 0, 0);
  3077. if (ret)
  3078. SetPageError(page);
  3079. }
  3080. cur = cur + iosize;
  3081. pg_offset += iosize;
  3082. nr++;
  3083. }
  3084. done:
  3085. *nr_ret = nr;
  3086. done_unlocked:
  3087. /* drop our reference on any cached states */
  3088. free_extent_state(cached_state);
  3089. return ret;
  3090. }
  3091. /*
  3092. * the writepage semantics are similar to regular writepage. extent
  3093. * records are inserted to lock ranges in the tree, and as dirty areas
  3094. * are found, they are marked writeback. Then the lock bits are removed
  3095. * and the end_io handler clears the writeback ranges
  3096. */
  3097. static int __extent_writepage(struct page *page, struct writeback_control *wbc,
  3098. void *data)
  3099. {
  3100. struct inode *inode = page->mapping->host;
  3101. struct extent_page_data *epd = data;
  3102. u64 start = page_offset(page);
  3103. u64 page_end = start + PAGE_CACHE_SIZE - 1;
  3104. int ret;
  3105. int nr = 0;
  3106. size_t pg_offset = 0;
  3107. loff_t i_size = i_size_read(inode);
  3108. unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
  3109. int write_flags;
  3110. unsigned long nr_written = 0;
  3111. if (wbc->sync_mode == WB_SYNC_ALL)
  3112. write_flags = WRITE_SYNC;
  3113. else
  3114. write_flags = WRITE;
  3115. trace___extent_writepage(page, inode, wbc);
  3116. WARN_ON(!PageLocked(page));
  3117. ClearPageError(page);
  3118. pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
  3119. if (page->index > end_index ||
  3120. (page->index == end_index && !pg_offset)) {
  3121. page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
  3122. unlock_page(page);
  3123. return 0;
  3124. }
  3125. if (page->index == end_index) {
  3126. char *userpage;
  3127. userpage = kmap_atomic(page);
  3128. memset(userpage + pg_offset, 0,
  3129. PAGE_CACHE_SIZE - pg_offset);
  3130. kunmap_atomic(userpage);
  3131. flush_dcache_page(page);
  3132. }
  3133. pg_offset = 0;
  3134. set_page_extent_mapped(page);
  3135. ret = writepage_delalloc(inode, page, wbc, epd, start, &nr_written);
  3136. if (ret == 1)
  3137. goto done_unlocked;
  3138. if (ret)
  3139. goto done;
  3140. ret = __extent_writepage_io(inode, page, wbc, epd,
  3141. i_size, nr_written, write_flags, &nr);
  3142. if (ret == 1)
  3143. goto done_unlocked;
  3144. done:
  3145. if (nr == 0) {
  3146. /* make sure the mapping tag for page dirty gets cleared */
  3147. set_page_writeback(page);
  3148. end_page_writeback(page);
  3149. }
  3150. if (PageError(page)) {
  3151. ret = ret < 0 ? ret : -EIO;
  3152. end_extent_writepage(page, ret, start, page_end);
  3153. }
  3154. unlock_page(page);
  3155. return ret;
  3156. done_unlocked:
  3157. return 0;
  3158. }
  3159. void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
  3160. {
  3161. wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_WRITEBACK,
  3162. TASK_UNINTERRUPTIBLE);
  3163. }
  3164. static noinline_for_stack int
  3165. lock_extent_buffer_for_io(struct extent_buffer *eb,
  3166. struct btrfs_fs_info *fs_info,
  3167. struct extent_page_data *epd)
  3168. {
  3169. unsigned long i, num_pages;
  3170. int flush = 0;
  3171. int ret = 0;
  3172. if (!btrfs_try_tree_write_lock(eb)) {
  3173. flush = 1;
  3174. flush_write_bio(epd);
  3175. btrfs_tree_lock(eb);
  3176. }
  3177. if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
  3178. btrfs_tree_unlock(eb);
  3179. if (!epd->sync_io)
  3180. return 0;
  3181. if (!flush) {
  3182. flush_write_bio(epd);
  3183. flush = 1;
  3184. }
  3185. while (1) {
  3186. wait_on_extent_buffer_writeback(eb);
  3187. btrfs_tree_lock(eb);
  3188. if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
  3189. break;
  3190. btrfs_tree_unlock(eb);
  3191. }
  3192. }
  3193. /*
  3194. * We need to do this to prevent races in people who check if the eb is
  3195. * under IO since we can end up having no IO bits set for a short period
  3196. * of time.
  3197. */
  3198. spin_lock(&eb->refs_lock);
  3199. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
  3200. set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3201. spin_unlock(&eb->refs_lock);
  3202. btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
  3203. __percpu_counter_add(&fs_info->dirty_metadata_bytes,
  3204. -eb->len,
  3205. fs_info->dirty_metadata_batch);
  3206. ret = 1;
  3207. } else {
  3208. spin_unlock(&eb->refs_lock);
  3209. }
  3210. btrfs_tree_unlock(eb);
  3211. if (!ret)
  3212. return ret;
  3213. num_pages = num_extent_pages(eb->start, eb->len);
  3214. for (i = 0; i < num_pages; i++) {
  3215. struct page *p = eb->pages[i];
  3216. if (!trylock_page(p)) {
  3217. if (!flush) {
  3218. flush_write_bio(epd);
  3219. flush = 1;
  3220. }
  3221. lock_page(p);
  3222. }
  3223. }
  3224. return ret;
  3225. }
  3226. static void end_extent_buffer_writeback(struct extent_buffer *eb)
  3227. {
  3228. clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
  3229. smp_mb__after_atomic();
  3230. wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
  3231. }
  3232. static void set_btree_ioerr(struct page *page)
  3233. {
  3234. struct extent_buffer *eb = (struct extent_buffer *)page->private;
  3235. struct btrfs_inode *btree_ino = BTRFS_I(eb->fs_info->btree_inode);
  3236. SetPageError(page);
  3237. if (test_and_set_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags))
  3238. return;
  3239. /*
  3240. * If writeback for a btree extent that doesn't belong to a log tree
  3241. * failed, increment the counter transaction->eb_write_errors.
  3242. * We do this because while the transaction is running and before it's
  3243. * committing (when we call filemap_fdata[write|wait]_range against
  3244. * the btree inode), we might have
  3245. * btree_inode->i_mapping->a_ops->writepages() called by the VM - if it
  3246. * returns an error or an error happens during writeback, when we're
  3247. * committing the transaction we wouldn't know about it, since the pages
  3248. * can be no longer dirty nor marked anymore for writeback (if a
  3249. * subsequent modification to the extent buffer didn't happen before the
  3250. * transaction commit), which makes filemap_fdata[write|wait]_range not
  3251. * able to find the pages tagged with SetPageError at transaction
  3252. * commit time. So if this happens we must abort the transaction,
  3253. * otherwise we commit a super block with btree roots that point to
  3254. * btree nodes/leafs whose content on disk is invalid - either garbage
  3255. * or the content of some node/leaf from a past generation that got
  3256. * cowed or deleted and is no longer valid.
  3257. *
  3258. * Note: setting AS_EIO/AS_ENOSPC in the btree inode's i_mapping would
  3259. * not be enough - we need to distinguish between log tree extents vs
  3260. * non-log tree extents, and the next filemap_fdatawait_range() call
  3261. * will catch and clear such errors in the mapping - and that call might
  3262. * be from a log sync and not from a transaction commit. Also, checking
  3263. * for the eb flag EXTENT_BUFFER_WRITE_ERR at transaction commit time is
  3264. * not done and would not be reliable - the eb might have been released
  3265. * from memory and reading it back again means that flag would not be
  3266. * set (since it's a runtime flag, not persisted on disk).
  3267. *
  3268. * Using the flags below in the btree inode also makes us achieve the
  3269. * goal of AS_EIO/AS_ENOSPC when writepages() returns success, started
  3270. * writeback for all dirty pages and before filemap_fdatawait_range()
  3271. * is called, the writeback for all dirty pages had already finished
  3272. * with errors - because we were not using AS_EIO/AS_ENOSPC,
  3273. * filemap_fdatawait_range() would return success, as it could not know
  3274. * that writeback errors happened (the pages were no longer tagged for
  3275. * writeback).
  3276. */
  3277. switch (eb->log_index) {
  3278. case -1:
  3279. set_bit(BTRFS_INODE_BTREE_ERR, &btree_ino->runtime_flags);
  3280. break;
  3281. case 0:
  3282. set_bit(BTRFS_INODE_BTREE_LOG1_ERR, &btree_ino->runtime_flags);
  3283. break;
  3284. case 1:
  3285. set_bit(BTRFS_INODE_BTREE_LOG2_ERR, &btree_ino->runtime_flags);
  3286. break;
  3287. default:
  3288. BUG(); /* unexpected, logic error */
  3289. }
  3290. }
  3291. static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
  3292. {
  3293. struct bio_vec *bvec;
  3294. struct extent_buffer *eb;
  3295. int i, done;
  3296. bio_for_each_segment_all(bvec, bio, i) {
  3297. struct page *page = bvec->bv_page;
  3298. eb = (struct extent_buffer *)page->private;
  3299. BUG_ON(!eb);
  3300. done = atomic_dec_and_test(&eb->io_pages);
  3301. if (err || test_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags)) {
  3302. ClearPageUptodate(page);
  3303. set_btree_ioerr(page);
  3304. }
  3305. end_page_writeback(page);
  3306. if (!done)
  3307. continue;
  3308. end_extent_buffer_writeback(eb);
  3309. }
  3310. bio_put(bio);
  3311. }
  3312. static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
  3313. struct btrfs_fs_info *fs_info,
  3314. struct writeback_control *wbc,
  3315. struct extent_page_data *epd)
  3316. {
  3317. struct block_device *bdev = fs_info->fs_devices->latest_bdev;
  3318. struct extent_io_tree *tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  3319. u64 offset = eb->start;
  3320. unsigned long i, num_pages;
  3321. unsigned long bio_flags = 0;
  3322. int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
  3323. int ret = 0;
  3324. clear_bit(EXTENT_BUFFER_WRITE_ERR, &eb->bflags);
  3325. num_pages = num_extent_pages(eb->start, eb->len);
  3326. atomic_set(&eb->io_pages, num_pages);
  3327. if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
  3328. bio_flags = EXTENT_BIO_TREE_LOG;
  3329. for (i = 0; i < num_pages; i++) {
  3330. struct page *p = eb->pages[i];
  3331. clear_page_dirty_for_io(p);
  3332. set_page_writeback(p);
  3333. ret = submit_extent_page(rw, tree, p, offset >> 9,
  3334. PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
  3335. -1, end_bio_extent_buffer_writepage,
  3336. 0, epd->bio_flags, bio_flags);
  3337. epd->bio_flags = bio_flags;
  3338. if (ret) {
  3339. set_btree_ioerr(p);
  3340. end_page_writeback(p);
  3341. if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
  3342. end_extent_buffer_writeback(eb);
  3343. ret = -EIO;
  3344. break;
  3345. }
  3346. offset += PAGE_CACHE_SIZE;
  3347. update_nr_written(p, wbc, 1);
  3348. unlock_page(p);
  3349. }
  3350. if (unlikely(ret)) {
  3351. for (; i < num_pages; i++) {
  3352. struct page *p = eb->pages[i];
  3353. clear_page_dirty_for_io(p);
  3354. unlock_page(p);
  3355. }
  3356. }
  3357. return ret;
  3358. }
  3359. int btree_write_cache_pages(struct address_space *mapping,
  3360. struct writeback_control *wbc)
  3361. {
  3362. struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
  3363. struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
  3364. struct extent_buffer *eb, *prev_eb = NULL;
  3365. struct extent_page_data epd = {
  3366. .bio = NULL,
  3367. .tree = tree,
  3368. .extent_locked = 0,
  3369. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3370. .bio_flags = 0,
  3371. };
  3372. int ret = 0;
  3373. int done = 0;
  3374. int nr_to_write_done = 0;
  3375. struct pagevec pvec;
  3376. int nr_pages;
  3377. pgoff_t index;
  3378. pgoff_t end; /* Inclusive */
  3379. int scanned = 0;
  3380. int tag;
  3381. pagevec_init(&pvec, 0);
  3382. if (wbc->range_cyclic) {
  3383. index = mapping->writeback_index; /* Start from prev offset */
  3384. end = -1;
  3385. } else {
  3386. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3387. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3388. scanned = 1;
  3389. }
  3390. if (wbc->sync_mode == WB_SYNC_ALL)
  3391. tag = PAGECACHE_TAG_TOWRITE;
  3392. else
  3393. tag = PAGECACHE_TAG_DIRTY;
  3394. retry:
  3395. if (wbc->sync_mode == WB_SYNC_ALL)
  3396. tag_pages_for_writeback(mapping, index, end);
  3397. while (!done && !nr_to_write_done && (index <= end) &&
  3398. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3399. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3400. unsigned i;
  3401. scanned = 1;
  3402. for (i = 0; i < nr_pages; i++) {
  3403. struct page *page = pvec.pages[i];
  3404. if (!PagePrivate(page))
  3405. continue;
  3406. if (!wbc->range_cyclic && page->index > end) {
  3407. done = 1;
  3408. break;
  3409. }
  3410. spin_lock(&mapping->private_lock);
  3411. if (!PagePrivate(page)) {
  3412. spin_unlock(&mapping->private_lock);
  3413. continue;
  3414. }
  3415. eb = (struct extent_buffer *)page->private;
  3416. /*
  3417. * Shouldn't happen and normally this would be a BUG_ON
  3418. * but no sense in crashing the users box for something
  3419. * we can survive anyway.
  3420. */
  3421. if (WARN_ON(!eb)) {
  3422. spin_unlock(&mapping->private_lock);
  3423. continue;
  3424. }
  3425. if (eb == prev_eb) {
  3426. spin_unlock(&mapping->private_lock);
  3427. continue;
  3428. }
  3429. ret = atomic_inc_not_zero(&eb->refs);
  3430. spin_unlock(&mapping->private_lock);
  3431. if (!ret)
  3432. continue;
  3433. prev_eb = eb;
  3434. ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
  3435. if (!ret) {
  3436. free_extent_buffer(eb);
  3437. continue;
  3438. }
  3439. ret = write_one_eb(eb, fs_info, wbc, &epd);
  3440. if (ret) {
  3441. done = 1;
  3442. free_extent_buffer(eb);
  3443. break;
  3444. }
  3445. free_extent_buffer(eb);
  3446. /*
  3447. * the filesystem may choose to bump up nr_to_write.
  3448. * We have to make sure to honor the new nr_to_write
  3449. * at any time
  3450. */
  3451. nr_to_write_done = wbc->nr_to_write <= 0;
  3452. }
  3453. pagevec_release(&pvec);
  3454. cond_resched();
  3455. }
  3456. if (!scanned && !done) {
  3457. /*
  3458. * We hit the last page and there is more work to be done: wrap
  3459. * back to the start of the file
  3460. */
  3461. scanned = 1;
  3462. index = 0;
  3463. goto retry;
  3464. }
  3465. flush_write_bio(&epd);
  3466. return ret;
  3467. }
  3468. /**
  3469. * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  3470. * @mapping: address space structure to write
  3471. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  3472. * @writepage: function called for each page
  3473. * @data: data passed to writepage function
  3474. *
  3475. * If a page is already under I/O, write_cache_pages() skips it, even
  3476. * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
  3477. * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
  3478. * and msync() need to guarantee that all the data which was dirty at the time
  3479. * the call was made get new I/O started against them. If wbc->sync_mode is
  3480. * WB_SYNC_ALL then we were called for data integrity and we must wait for
  3481. * existing IO to complete.
  3482. */
  3483. static int extent_write_cache_pages(struct extent_io_tree *tree,
  3484. struct address_space *mapping,
  3485. struct writeback_control *wbc,
  3486. writepage_t writepage, void *data,
  3487. void (*flush_fn)(void *))
  3488. {
  3489. struct inode *inode = mapping->host;
  3490. int ret = 0;
  3491. int done = 0;
  3492. int err = 0;
  3493. int nr_to_write_done = 0;
  3494. struct pagevec pvec;
  3495. int nr_pages;
  3496. pgoff_t index;
  3497. pgoff_t end; /* Inclusive */
  3498. int scanned = 0;
  3499. int tag;
  3500. /*
  3501. * We have to hold onto the inode so that ordered extents can do their
  3502. * work when the IO finishes. The alternative to this is failing to add
  3503. * an ordered extent if the igrab() fails there and that is a huge pain
  3504. * to deal with, so instead just hold onto the inode throughout the
  3505. * writepages operation. If it fails here we are freeing up the inode
  3506. * anyway and we'd rather not waste our time writing out stuff that is
  3507. * going to be truncated anyway.
  3508. */
  3509. if (!igrab(inode))
  3510. return 0;
  3511. pagevec_init(&pvec, 0);
  3512. if (wbc->range_cyclic) {
  3513. index = mapping->writeback_index; /* Start from prev offset */
  3514. end = -1;
  3515. } else {
  3516. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  3517. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  3518. scanned = 1;
  3519. }
  3520. if (wbc->sync_mode == WB_SYNC_ALL)
  3521. tag = PAGECACHE_TAG_TOWRITE;
  3522. else
  3523. tag = PAGECACHE_TAG_DIRTY;
  3524. retry:
  3525. if (wbc->sync_mode == WB_SYNC_ALL)
  3526. tag_pages_for_writeback(mapping, index, end);
  3527. while (!done && !nr_to_write_done && (index <= end) &&
  3528. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  3529. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  3530. unsigned i;
  3531. scanned = 1;
  3532. for (i = 0; i < nr_pages; i++) {
  3533. struct page *page = pvec.pages[i];
  3534. /*
  3535. * At this point we hold neither mapping->tree_lock nor
  3536. * lock on the page itself: the page may be truncated or
  3537. * invalidated (changing page->mapping to NULL), or even
  3538. * swizzled back from swapper_space to tmpfs file
  3539. * mapping
  3540. */
  3541. if (!trylock_page(page)) {
  3542. flush_fn(data);
  3543. lock_page(page);
  3544. }
  3545. if (unlikely(page->mapping != mapping)) {
  3546. unlock_page(page);
  3547. continue;
  3548. }
  3549. if (!wbc->range_cyclic && page->index > end) {
  3550. done = 1;
  3551. unlock_page(page);
  3552. continue;
  3553. }
  3554. if (wbc->sync_mode != WB_SYNC_NONE) {
  3555. if (PageWriteback(page))
  3556. flush_fn(data);
  3557. wait_on_page_writeback(page);
  3558. }
  3559. if (PageWriteback(page) ||
  3560. !clear_page_dirty_for_io(page)) {
  3561. unlock_page(page);
  3562. continue;
  3563. }
  3564. ret = (*writepage)(page, wbc, data);
  3565. if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
  3566. unlock_page(page);
  3567. ret = 0;
  3568. }
  3569. if (!err && ret < 0)
  3570. err = ret;
  3571. /*
  3572. * the filesystem may choose to bump up nr_to_write.
  3573. * We have to make sure to honor the new nr_to_write
  3574. * at any time
  3575. */
  3576. nr_to_write_done = wbc->nr_to_write <= 0;
  3577. }
  3578. pagevec_release(&pvec);
  3579. cond_resched();
  3580. }
  3581. if (!scanned && !done && !err) {
  3582. /*
  3583. * We hit the last page and there is more work to be done: wrap
  3584. * back to the start of the file
  3585. */
  3586. scanned = 1;
  3587. index = 0;
  3588. goto retry;
  3589. }
  3590. btrfs_add_delayed_iput(inode);
  3591. return err;
  3592. }
  3593. static void flush_epd_write_bio(struct extent_page_data *epd)
  3594. {
  3595. if (epd->bio) {
  3596. int rw = WRITE;
  3597. int ret;
  3598. if (epd->sync_io)
  3599. rw = WRITE_SYNC;
  3600. ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
  3601. BUG_ON(ret < 0); /* -ENOMEM */
  3602. epd->bio = NULL;
  3603. }
  3604. }
  3605. static noinline void flush_write_bio(void *data)
  3606. {
  3607. struct extent_page_data *epd = data;
  3608. flush_epd_write_bio(epd);
  3609. }
  3610. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  3611. get_extent_t *get_extent,
  3612. struct writeback_control *wbc)
  3613. {
  3614. int ret;
  3615. struct extent_page_data epd = {
  3616. .bio = NULL,
  3617. .tree = tree,
  3618. .get_extent = get_extent,
  3619. .extent_locked = 0,
  3620. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3621. .bio_flags = 0,
  3622. };
  3623. ret = __extent_writepage(page, wbc, &epd);
  3624. flush_epd_write_bio(&epd);
  3625. return ret;
  3626. }
  3627. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  3628. u64 start, u64 end, get_extent_t *get_extent,
  3629. int mode)
  3630. {
  3631. int ret = 0;
  3632. struct address_space *mapping = inode->i_mapping;
  3633. struct page *page;
  3634. unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
  3635. PAGE_CACHE_SHIFT;
  3636. struct extent_page_data epd = {
  3637. .bio = NULL,
  3638. .tree = tree,
  3639. .get_extent = get_extent,
  3640. .extent_locked = 1,
  3641. .sync_io = mode == WB_SYNC_ALL,
  3642. .bio_flags = 0,
  3643. };
  3644. struct writeback_control wbc_writepages = {
  3645. .sync_mode = mode,
  3646. .nr_to_write = nr_pages * 2,
  3647. .range_start = start,
  3648. .range_end = end + 1,
  3649. };
  3650. while (start <= end) {
  3651. page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
  3652. if (clear_page_dirty_for_io(page))
  3653. ret = __extent_writepage(page, &wbc_writepages, &epd);
  3654. else {
  3655. if (tree->ops && tree->ops->writepage_end_io_hook)
  3656. tree->ops->writepage_end_io_hook(page, start,
  3657. start + PAGE_CACHE_SIZE - 1,
  3658. NULL, 1);
  3659. unlock_page(page);
  3660. }
  3661. page_cache_release(page);
  3662. start += PAGE_CACHE_SIZE;
  3663. }
  3664. flush_epd_write_bio(&epd);
  3665. return ret;
  3666. }
  3667. int extent_writepages(struct extent_io_tree *tree,
  3668. struct address_space *mapping,
  3669. get_extent_t *get_extent,
  3670. struct writeback_control *wbc)
  3671. {
  3672. int ret = 0;
  3673. struct extent_page_data epd = {
  3674. .bio = NULL,
  3675. .tree = tree,
  3676. .get_extent = get_extent,
  3677. .extent_locked = 0,
  3678. .sync_io = wbc->sync_mode == WB_SYNC_ALL,
  3679. .bio_flags = 0,
  3680. };
  3681. ret = extent_write_cache_pages(tree, mapping, wbc,
  3682. __extent_writepage, &epd,
  3683. flush_write_bio);
  3684. flush_epd_write_bio(&epd);
  3685. return ret;
  3686. }
  3687. int extent_readpages(struct extent_io_tree *tree,
  3688. struct address_space *mapping,
  3689. struct list_head *pages, unsigned nr_pages,
  3690. get_extent_t get_extent)
  3691. {
  3692. struct bio *bio = NULL;
  3693. unsigned page_idx;
  3694. unsigned long bio_flags = 0;
  3695. struct page *pagepool[16];
  3696. struct page *page;
  3697. struct extent_map *em_cached = NULL;
  3698. int nr = 0;
  3699. for (page_idx = 0; page_idx < nr_pages; page_idx++) {
  3700. page = list_entry(pages->prev, struct page, lru);
  3701. prefetchw(&page->flags);
  3702. list_del(&page->lru);
  3703. if (add_to_page_cache_lru(page, mapping,
  3704. page->index, GFP_NOFS)) {
  3705. page_cache_release(page);
  3706. continue;
  3707. }
  3708. pagepool[nr++] = page;
  3709. if (nr < ARRAY_SIZE(pagepool))
  3710. continue;
  3711. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3712. &bio, 0, &bio_flags, READ);
  3713. nr = 0;
  3714. }
  3715. if (nr)
  3716. __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
  3717. &bio, 0, &bio_flags, READ);
  3718. if (em_cached)
  3719. free_extent_map(em_cached);
  3720. BUG_ON(!list_empty(pages));
  3721. if (bio)
  3722. return submit_one_bio(READ, bio, 0, bio_flags);
  3723. return 0;
  3724. }
  3725. /*
  3726. * basic invalidatepage code, this waits on any locked or writeback
  3727. * ranges corresponding to the page, and then deletes any extent state
  3728. * records from the tree
  3729. */
  3730. int extent_invalidatepage(struct extent_io_tree *tree,
  3731. struct page *page, unsigned long offset)
  3732. {
  3733. struct extent_state *cached_state = NULL;
  3734. u64 start = page_offset(page);
  3735. u64 end = start + PAGE_CACHE_SIZE - 1;
  3736. size_t blocksize = page->mapping->host->i_sb->s_blocksize;
  3737. start += ALIGN(offset, blocksize);
  3738. if (start > end)
  3739. return 0;
  3740. lock_extent_bits(tree, start, end, 0, &cached_state);
  3741. wait_on_page_writeback(page);
  3742. clear_extent_bit(tree, start, end,
  3743. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3744. EXTENT_DO_ACCOUNTING,
  3745. 1, 1, &cached_state, GFP_NOFS);
  3746. return 0;
  3747. }
  3748. /*
  3749. * a helper for releasepage, this tests for areas of the page that
  3750. * are locked or under IO and drops the related state bits if it is safe
  3751. * to drop the page.
  3752. */
  3753. static int try_release_extent_state(struct extent_map_tree *map,
  3754. struct extent_io_tree *tree,
  3755. struct page *page, gfp_t mask)
  3756. {
  3757. u64 start = page_offset(page);
  3758. u64 end = start + PAGE_CACHE_SIZE - 1;
  3759. int ret = 1;
  3760. if (test_range_bit(tree, start, end,
  3761. EXTENT_IOBITS, 0, NULL))
  3762. ret = 0;
  3763. else {
  3764. if ((mask & GFP_NOFS) == GFP_NOFS)
  3765. mask = GFP_NOFS;
  3766. /*
  3767. * at this point we can safely clear everything except the
  3768. * locked bit and the nodatasum bit
  3769. */
  3770. ret = clear_extent_bit(tree, start, end,
  3771. ~(EXTENT_LOCKED | EXTENT_NODATASUM),
  3772. 0, 0, NULL, mask);
  3773. /* if clear_extent_bit failed for enomem reasons,
  3774. * we can't allow the release to continue.
  3775. */
  3776. if (ret < 0)
  3777. ret = 0;
  3778. else
  3779. ret = 1;
  3780. }
  3781. return ret;
  3782. }
  3783. /*
  3784. * a helper for releasepage. As long as there are no locked extents
  3785. * in the range corresponding to the page, both state records and extent
  3786. * map records are removed
  3787. */
  3788. int try_release_extent_mapping(struct extent_map_tree *map,
  3789. struct extent_io_tree *tree, struct page *page,
  3790. gfp_t mask)
  3791. {
  3792. struct extent_map *em;
  3793. u64 start = page_offset(page);
  3794. u64 end = start + PAGE_CACHE_SIZE - 1;
  3795. if ((mask & __GFP_WAIT) &&
  3796. page->mapping->host->i_size > 16 * 1024 * 1024) {
  3797. u64 len;
  3798. while (start <= end) {
  3799. len = end - start + 1;
  3800. write_lock(&map->lock);
  3801. em = lookup_extent_mapping(map, start, len);
  3802. if (!em) {
  3803. write_unlock(&map->lock);
  3804. break;
  3805. }
  3806. if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
  3807. em->start != start) {
  3808. write_unlock(&map->lock);
  3809. free_extent_map(em);
  3810. break;
  3811. }
  3812. if (!test_range_bit(tree, em->start,
  3813. extent_map_end(em) - 1,
  3814. EXTENT_LOCKED | EXTENT_WRITEBACK,
  3815. 0, NULL)) {
  3816. remove_extent_mapping(map, em);
  3817. /* once for the rb tree */
  3818. free_extent_map(em);
  3819. }
  3820. start = extent_map_end(em);
  3821. write_unlock(&map->lock);
  3822. /* once for us */
  3823. free_extent_map(em);
  3824. }
  3825. }
  3826. return try_release_extent_state(map, tree, page, mask);
  3827. }
  3828. /*
  3829. * helper function for fiemap, which doesn't want to see any holes.
  3830. * This maps until we find something past 'last'
  3831. */
  3832. static struct extent_map *get_extent_skip_holes(struct inode *inode,
  3833. u64 offset,
  3834. u64 last,
  3835. get_extent_t *get_extent)
  3836. {
  3837. u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
  3838. struct extent_map *em;
  3839. u64 len;
  3840. if (offset >= last)
  3841. return NULL;
  3842. while (1) {
  3843. len = last - offset;
  3844. if (len == 0)
  3845. break;
  3846. len = ALIGN(len, sectorsize);
  3847. em = get_extent(inode, NULL, 0, offset, len, 0);
  3848. if (IS_ERR_OR_NULL(em))
  3849. return em;
  3850. /* if this isn't a hole return it */
  3851. if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
  3852. em->block_start != EXTENT_MAP_HOLE) {
  3853. return em;
  3854. }
  3855. /* this is a hole, advance to the next extent */
  3856. offset = extent_map_end(em);
  3857. free_extent_map(em);
  3858. if (offset >= last)
  3859. break;
  3860. }
  3861. return NULL;
  3862. }
  3863. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3864. __u64 start, __u64 len, get_extent_t *get_extent)
  3865. {
  3866. int ret = 0;
  3867. u64 off = start;
  3868. u64 max = start + len;
  3869. u32 flags = 0;
  3870. u32 found_type;
  3871. u64 last;
  3872. u64 last_for_get_extent = 0;
  3873. u64 disko = 0;
  3874. u64 isize = i_size_read(inode);
  3875. struct btrfs_key found_key;
  3876. struct extent_map *em = NULL;
  3877. struct extent_state *cached_state = NULL;
  3878. struct btrfs_path *path;
  3879. struct btrfs_root *root = BTRFS_I(inode)->root;
  3880. int end = 0;
  3881. u64 em_start = 0;
  3882. u64 em_len = 0;
  3883. u64 em_end = 0;
  3884. if (len == 0)
  3885. return -EINVAL;
  3886. path = btrfs_alloc_path();
  3887. if (!path)
  3888. return -ENOMEM;
  3889. path->leave_spinning = 1;
  3890. start = round_down(start, BTRFS_I(inode)->root->sectorsize);
  3891. len = round_up(max, BTRFS_I(inode)->root->sectorsize) - start;
  3892. /*
  3893. * lookup the last file extent. We're not using i_size here
  3894. * because there might be preallocation past i_size
  3895. */
  3896. ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), -1,
  3897. 0);
  3898. if (ret < 0) {
  3899. btrfs_free_path(path);
  3900. return ret;
  3901. }
  3902. WARN_ON(!ret);
  3903. path->slots[0]--;
  3904. btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
  3905. found_type = found_key.type;
  3906. /* No extents, but there might be delalloc bits */
  3907. if (found_key.objectid != btrfs_ino(inode) ||
  3908. found_type != BTRFS_EXTENT_DATA_KEY) {
  3909. /* have to trust i_size as the end */
  3910. last = (u64)-1;
  3911. last_for_get_extent = isize;
  3912. } else {
  3913. /*
  3914. * remember the start of the last extent. There are a
  3915. * bunch of different factors that go into the length of the
  3916. * extent, so its much less complex to remember where it started
  3917. */
  3918. last = found_key.offset;
  3919. last_for_get_extent = last + 1;
  3920. }
  3921. btrfs_release_path(path);
  3922. /*
  3923. * we might have some extents allocated but more delalloc past those
  3924. * extents. so, we trust isize unless the start of the last extent is
  3925. * beyond isize
  3926. */
  3927. if (last < isize) {
  3928. last = (u64)-1;
  3929. last_for_get_extent = isize;
  3930. }
  3931. lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
  3932. &cached_state);
  3933. em = get_extent_skip_holes(inode, start, last_for_get_extent,
  3934. get_extent);
  3935. if (!em)
  3936. goto out;
  3937. if (IS_ERR(em)) {
  3938. ret = PTR_ERR(em);
  3939. goto out;
  3940. }
  3941. while (!end) {
  3942. u64 offset_in_extent = 0;
  3943. /* break if the extent we found is outside the range */
  3944. if (em->start >= max || extent_map_end(em) < off)
  3945. break;
  3946. /*
  3947. * get_extent may return an extent that starts before our
  3948. * requested range. We have to make sure the ranges
  3949. * we return to fiemap always move forward and don't
  3950. * overlap, so adjust the offsets here
  3951. */
  3952. em_start = max(em->start, off);
  3953. /*
  3954. * record the offset from the start of the extent
  3955. * for adjusting the disk offset below. Only do this if the
  3956. * extent isn't compressed since our in ram offset may be past
  3957. * what we have actually allocated on disk.
  3958. */
  3959. if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  3960. offset_in_extent = em_start - em->start;
  3961. em_end = extent_map_end(em);
  3962. em_len = em_end - em_start;
  3963. disko = 0;
  3964. flags = 0;
  3965. /*
  3966. * bump off for our next call to get_extent
  3967. */
  3968. off = extent_map_end(em);
  3969. if (off >= max)
  3970. end = 1;
  3971. if (em->block_start == EXTENT_MAP_LAST_BYTE) {
  3972. end = 1;
  3973. flags |= FIEMAP_EXTENT_LAST;
  3974. } else if (em->block_start == EXTENT_MAP_INLINE) {
  3975. flags |= (FIEMAP_EXTENT_DATA_INLINE |
  3976. FIEMAP_EXTENT_NOT_ALIGNED);
  3977. } else if (em->block_start == EXTENT_MAP_DELALLOC) {
  3978. flags |= (FIEMAP_EXTENT_DELALLOC |
  3979. FIEMAP_EXTENT_UNKNOWN);
  3980. } else if (fieinfo->fi_extents_max) {
  3981. u64 bytenr = em->block_start -
  3982. (em->start - em->orig_start);
  3983. disko = em->block_start + offset_in_extent;
  3984. /*
  3985. * As btrfs supports shared space, this information
  3986. * can be exported to userspace tools via
  3987. * flag FIEMAP_EXTENT_SHARED. If fi_extents_max == 0
  3988. * then we're just getting a count and we can skip the
  3989. * lookup stuff.
  3990. */
  3991. ret = btrfs_check_shared(NULL, root->fs_info,
  3992. root->objectid,
  3993. btrfs_ino(inode), bytenr);
  3994. if (ret < 0)
  3995. goto out_free;
  3996. if (ret)
  3997. flags |= FIEMAP_EXTENT_SHARED;
  3998. ret = 0;
  3999. }
  4000. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
  4001. flags |= FIEMAP_EXTENT_ENCODED;
  4002. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  4003. flags |= FIEMAP_EXTENT_UNWRITTEN;
  4004. free_extent_map(em);
  4005. em = NULL;
  4006. if ((em_start >= last) || em_len == (u64)-1 ||
  4007. (last == (u64)-1 && isize <= em_end)) {
  4008. flags |= FIEMAP_EXTENT_LAST;
  4009. end = 1;
  4010. }
  4011. /* now scan forward to see if this is really the last extent. */
  4012. em = get_extent_skip_holes(inode, off, last_for_get_extent,
  4013. get_extent);
  4014. if (IS_ERR(em)) {
  4015. ret = PTR_ERR(em);
  4016. goto out;
  4017. }
  4018. if (!em) {
  4019. flags |= FIEMAP_EXTENT_LAST;
  4020. end = 1;
  4021. }
  4022. ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
  4023. em_len, flags);
  4024. if (ret) {
  4025. if (ret == 1)
  4026. ret = 0;
  4027. goto out_free;
  4028. }
  4029. }
  4030. out_free:
  4031. free_extent_map(em);
  4032. out:
  4033. btrfs_free_path(path);
  4034. unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
  4035. &cached_state, GFP_NOFS);
  4036. return ret;
  4037. }
  4038. static void __free_extent_buffer(struct extent_buffer *eb)
  4039. {
  4040. btrfs_leak_debug_del(&eb->leak_list);
  4041. kmem_cache_free(extent_buffer_cache, eb);
  4042. }
  4043. int extent_buffer_under_io(struct extent_buffer *eb)
  4044. {
  4045. return (atomic_read(&eb->io_pages) ||
  4046. test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
  4047. test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4048. }
  4049. /*
  4050. * Helper for releasing extent buffer page.
  4051. */
  4052. static void btrfs_release_extent_buffer_page(struct extent_buffer *eb)
  4053. {
  4054. unsigned long index;
  4055. struct page *page;
  4056. int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4057. BUG_ON(extent_buffer_under_io(eb));
  4058. index = num_extent_pages(eb->start, eb->len);
  4059. if (index == 0)
  4060. return;
  4061. do {
  4062. index--;
  4063. page = eb->pages[index];
  4064. if (!page)
  4065. continue;
  4066. if (mapped)
  4067. spin_lock(&page->mapping->private_lock);
  4068. /*
  4069. * We do this since we'll remove the pages after we've
  4070. * removed the eb from the radix tree, so we could race
  4071. * and have this page now attached to the new eb. So
  4072. * only clear page_private if it's still connected to
  4073. * this eb.
  4074. */
  4075. if (PagePrivate(page) &&
  4076. page->private == (unsigned long)eb) {
  4077. BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  4078. BUG_ON(PageDirty(page));
  4079. BUG_ON(PageWriteback(page));
  4080. /*
  4081. * We need to make sure we haven't be attached
  4082. * to a new eb.
  4083. */
  4084. ClearPagePrivate(page);
  4085. set_page_private(page, 0);
  4086. /* One for the page private */
  4087. page_cache_release(page);
  4088. }
  4089. if (mapped)
  4090. spin_unlock(&page->mapping->private_lock);
  4091. /* One for when we alloced the page */
  4092. page_cache_release(page);
  4093. } while (index != 0);
  4094. }
  4095. /*
  4096. * Helper for releasing the extent buffer.
  4097. */
  4098. static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
  4099. {
  4100. btrfs_release_extent_buffer_page(eb);
  4101. __free_extent_buffer(eb);
  4102. }
  4103. static struct extent_buffer *
  4104. __alloc_extent_buffer(struct btrfs_fs_info *fs_info, u64 start,
  4105. unsigned long len)
  4106. {
  4107. struct extent_buffer *eb = NULL;
  4108. eb = kmem_cache_zalloc(extent_buffer_cache, GFP_NOFS);
  4109. if (eb == NULL)
  4110. return NULL;
  4111. eb->start = start;
  4112. eb->len = len;
  4113. eb->fs_info = fs_info;
  4114. eb->bflags = 0;
  4115. rwlock_init(&eb->lock);
  4116. atomic_set(&eb->write_locks, 0);
  4117. atomic_set(&eb->read_locks, 0);
  4118. atomic_set(&eb->blocking_readers, 0);
  4119. atomic_set(&eb->blocking_writers, 0);
  4120. atomic_set(&eb->spinning_readers, 0);
  4121. atomic_set(&eb->spinning_writers, 0);
  4122. eb->lock_nested = 0;
  4123. init_waitqueue_head(&eb->write_lock_wq);
  4124. init_waitqueue_head(&eb->read_lock_wq);
  4125. btrfs_leak_debug_add(&eb->leak_list, &buffers);
  4126. spin_lock_init(&eb->refs_lock);
  4127. atomic_set(&eb->refs, 1);
  4128. atomic_set(&eb->io_pages, 0);
  4129. /*
  4130. * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
  4131. */
  4132. BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
  4133. > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4134. BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
  4135. return eb;
  4136. }
  4137. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
  4138. {
  4139. unsigned long i;
  4140. struct page *p;
  4141. struct extent_buffer *new;
  4142. unsigned long num_pages = num_extent_pages(src->start, src->len);
  4143. new = __alloc_extent_buffer(src->fs_info, src->start, src->len);
  4144. if (new == NULL)
  4145. return NULL;
  4146. for (i = 0; i < num_pages; i++) {
  4147. p = alloc_page(GFP_NOFS);
  4148. if (!p) {
  4149. btrfs_release_extent_buffer(new);
  4150. return NULL;
  4151. }
  4152. attach_extent_buffer_page(new, p);
  4153. WARN_ON(PageDirty(p));
  4154. SetPageUptodate(p);
  4155. new->pages[i] = p;
  4156. }
  4157. copy_extent_buffer(new, src, 0, 0, src->len);
  4158. set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
  4159. set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
  4160. return new;
  4161. }
  4162. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  4163. u64 start)
  4164. {
  4165. struct extent_buffer *eb;
  4166. unsigned long len;
  4167. unsigned long num_pages;
  4168. unsigned long i;
  4169. if (!fs_info) {
  4170. /*
  4171. * Called only from tests that don't always have a fs_info
  4172. * available, but we know that nodesize is 4096
  4173. */
  4174. len = 4096;
  4175. } else {
  4176. len = fs_info->tree_root->nodesize;
  4177. }
  4178. num_pages = num_extent_pages(0, len);
  4179. eb = __alloc_extent_buffer(fs_info, start, len);
  4180. if (!eb)
  4181. return NULL;
  4182. for (i = 0; i < num_pages; i++) {
  4183. eb->pages[i] = alloc_page(GFP_NOFS);
  4184. if (!eb->pages[i])
  4185. goto err;
  4186. }
  4187. set_extent_buffer_uptodate(eb);
  4188. btrfs_set_header_nritems(eb, 0);
  4189. set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
  4190. return eb;
  4191. err:
  4192. for (; i > 0; i--)
  4193. __free_page(eb->pages[i - 1]);
  4194. __free_extent_buffer(eb);
  4195. return NULL;
  4196. }
  4197. static void check_buffer_tree_ref(struct extent_buffer *eb)
  4198. {
  4199. int refs;
  4200. /* the ref bit is tricky. We have to make sure it is set
  4201. * if we have the buffer dirty. Otherwise the
  4202. * code to free a buffer can end up dropping a dirty
  4203. * page
  4204. *
  4205. * Once the ref bit is set, it won't go away while the
  4206. * buffer is dirty or in writeback, and it also won't
  4207. * go away while we have the reference count on the
  4208. * eb bumped.
  4209. *
  4210. * We can't just set the ref bit without bumping the
  4211. * ref on the eb because free_extent_buffer might
  4212. * see the ref bit and try to clear it. If this happens
  4213. * free_extent_buffer might end up dropping our original
  4214. * ref by mistake and freeing the page before we are able
  4215. * to add one more ref.
  4216. *
  4217. * So bump the ref count first, then set the bit. If someone
  4218. * beat us to it, drop the ref we added.
  4219. */
  4220. refs = atomic_read(&eb->refs);
  4221. if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4222. return;
  4223. spin_lock(&eb->refs_lock);
  4224. if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4225. atomic_inc(&eb->refs);
  4226. spin_unlock(&eb->refs_lock);
  4227. }
  4228. static void mark_extent_buffer_accessed(struct extent_buffer *eb,
  4229. struct page *accessed)
  4230. {
  4231. unsigned long num_pages, i;
  4232. check_buffer_tree_ref(eb);
  4233. num_pages = num_extent_pages(eb->start, eb->len);
  4234. for (i = 0; i < num_pages; i++) {
  4235. struct page *p = eb->pages[i];
  4236. if (p != accessed)
  4237. mark_page_accessed(p);
  4238. }
  4239. }
  4240. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  4241. u64 start)
  4242. {
  4243. struct extent_buffer *eb;
  4244. rcu_read_lock();
  4245. eb = radix_tree_lookup(&fs_info->buffer_radix,
  4246. start >> PAGE_CACHE_SHIFT);
  4247. if (eb && atomic_inc_not_zero(&eb->refs)) {
  4248. rcu_read_unlock();
  4249. /*
  4250. * Lock our eb's refs_lock to avoid races with
  4251. * free_extent_buffer. When we get our eb it might be flagged
  4252. * with EXTENT_BUFFER_STALE and another task running
  4253. * free_extent_buffer might have seen that flag set,
  4254. * eb->refs == 2, that the buffer isn't under IO (dirty and
  4255. * writeback flags not set) and it's still in the tree (flag
  4256. * EXTENT_BUFFER_TREE_REF set), therefore being in the process
  4257. * of decrementing the extent buffer's reference count twice.
  4258. * So here we could race and increment the eb's reference count,
  4259. * clear its stale flag, mark it as dirty and drop our reference
  4260. * before the other task finishes executing free_extent_buffer,
  4261. * which would later result in an attempt to free an extent
  4262. * buffer that is dirty.
  4263. */
  4264. if (test_bit(EXTENT_BUFFER_STALE, &eb->bflags)) {
  4265. spin_lock(&eb->refs_lock);
  4266. spin_unlock(&eb->refs_lock);
  4267. }
  4268. mark_extent_buffer_accessed(eb, NULL);
  4269. return eb;
  4270. }
  4271. rcu_read_unlock();
  4272. return NULL;
  4273. }
  4274. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4275. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  4276. u64 start)
  4277. {
  4278. struct extent_buffer *eb, *exists = NULL;
  4279. int ret;
  4280. eb = find_extent_buffer(fs_info, start);
  4281. if (eb)
  4282. return eb;
  4283. eb = alloc_dummy_extent_buffer(fs_info, start);
  4284. if (!eb)
  4285. return NULL;
  4286. eb->fs_info = fs_info;
  4287. again:
  4288. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  4289. if (ret)
  4290. goto free_eb;
  4291. spin_lock(&fs_info->buffer_lock);
  4292. ret = radix_tree_insert(&fs_info->buffer_radix,
  4293. start >> PAGE_CACHE_SHIFT, eb);
  4294. spin_unlock(&fs_info->buffer_lock);
  4295. radix_tree_preload_end();
  4296. if (ret == -EEXIST) {
  4297. exists = find_extent_buffer(fs_info, start);
  4298. if (exists)
  4299. goto free_eb;
  4300. else
  4301. goto again;
  4302. }
  4303. check_buffer_tree_ref(eb);
  4304. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4305. /*
  4306. * We will free dummy extent buffer's if they come into
  4307. * free_extent_buffer with a ref count of 2, but if we are using this we
  4308. * want the buffers to stay in memory until we're done with them, so
  4309. * bump the ref count again.
  4310. */
  4311. atomic_inc(&eb->refs);
  4312. return eb;
  4313. free_eb:
  4314. btrfs_release_extent_buffer(eb);
  4315. return exists;
  4316. }
  4317. #endif
  4318. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  4319. u64 start)
  4320. {
  4321. unsigned long len = fs_info->tree_root->nodesize;
  4322. unsigned long num_pages = num_extent_pages(start, len);
  4323. unsigned long i;
  4324. unsigned long index = start >> PAGE_CACHE_SHIFT;
  4325. struct extent_buffer *eb;
  4326. struct extent_buffer *exists = NULL;
  4327. struct page *p;
  4328. struct address_space *mapping = fs_info->btree_inode->i_mapping;
  4329. int uptodate = 1;
  4330. int ret;
  4331. eb = find_extent_buffer(fs_info, start);
  4332. if (eb)
  4333. return eb;
  4334. eb = __alloc_extent_buffer(fs_info, start, len);
  4335. if (!eb)
  4336. return NULL;
  4337. for (i = 0; i < num_pages; i++, index++) {
  4338. p = find_or_create_page(mapping, index, GFP_NOFS);
  4339. if (!p)
  4340. goto free_eb;
  4341. spin_lock(&mapping->private_lock);
  4342. if (PagePrivate(p)) {
  4343. /*
  4344. * We could have already allocated an eb for this page
  4345. * and attached one so lets see if we can get a ref on
  4346. * the existing eb, and if we can we know it's good and
  4347. * we can just return that one, else we know we can just
  4348. * overwrite page->private.
  4349. */
  4350. exists = (struct extent_buffer *)p->private;
  4351. if (atomic_inc_not_zero(&exists->refs)) {
  4352. spin_unlock(&mapping->private_lock);
  4353. unlock_page(p);
  4354. page_cache_release(p);
  4355. mark_extent_buffer_accessed(exists, p);
  4356. goto free_eb;
  4357. }
  4358. exists = NULL;
  4359. /*
  4360. * Do this so attach doesn't complain and we need to
  4361. * drop the ref the old guy had.
  4362. */
  4363. ClearPagePrivate(p);
  4364. WARN_ON(PageDirty(p));
  4365. page_cache_release(p);
  4366. }
  4367. attach_extent_buffer_page(eb, p);
  4368. spin_unlock(&mapping->private_lock);
  4369. WARN_ON(PageDirty(p));
  4370. eb->pages[i] = p;
  4371. if (!PageUptodate(p))
  4372. uptodate = 0;
  4373. /*
  4374. * see below about how we avoid a nasty race with release page
  4375. * and why we unlock later
  4376. */
  4377. }
  4378. if (uptodate)
  4379. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4380. again:
  4381. ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  4382. if (ret)
  4383. goto free_eb;
  4384. spin_lock(&fs_info->buffer_lock);
  4385. ret = radix_tree_insert(&fs_info->buffer_radix,
  4386. start >> PAGE_CACHE_SHIFT, eb);
  4387. spin_unlock(&fs_info->buffer_lock);
  4388. radix_tree_preload_end();
  4389. if (ret == -EEXIST) {
  4390. exists = find_extent_buffer(fs_info, start);
  4391. if (exists)
  4392. goto free_eb;
  4393. else
  4394. goto again;
  4395. }
  4396. /* add one reference for the tree */
  4397. check_buffer_tree_ref(eb);
  4398. set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
  4399. /*
  4400. * there is a race where release page may have
  4401. * tried to find this extent buffer in the radix
  4402. * but failed. It will tell the VM it is safe to
  4403. * reclaim the, and it will clear the page private bit.
  4404. * We must make sure to set the page private bit properly
  4405. * after the extent buffer is in the radix tree so
  4406. * it doesn't get lost
  4407. */
  4408. SetPageChecked(eb->pages[0]);
  4409. for (i = 1; i < num_pages; i++) {
  4410. p = eb->pages[i];
  4411. ClearPageChecked(p);
  4412. unlock_page(p);
  4413. }
  4414. unlock_page(eb->pages[0]);
  4415. return eb;
  4416. free_eb:
  4417. WARN_ON(!atomic_dec_and_test(&eb->refs));
  4418. for (i = 0; i < num_pages; i++) {
  4419. if (eb->pages[i])
  4420. unlock_page(eb->pages[i]);
  4421. }
  4422. btrfs_release_extent_buffer(eb);
  4423. return exists;
  4424. }
  4425. static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
  4426. {
  4427. struct extent_buffer *eb =
  4428. container_of(head, struct extent_buffer, rcu_head);
  4429. __free_extent_buffer(eb);
  4430. }
  4431. /* Expects to have eb->eb_lock already held */
  4432. static int release_extent_buffer(struct extent_buffer *eb)
  4433. {
  4434. WARN_ON(atomic_read(&eb->refs) == 0);
  4435. if (atomic_dec_and_test(&eb->refs)) {
  4436. if (test_and_clear_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags)) {
  4437. struct btrfs_fs_info *fs_info = eb->fs_info;
  4438. spin_unlock(&eb->refs_lock);
  4439. spin_lock(&fs_info->buffer_lock);
  4440. radix_tree_delete(&fs_info->buffer_radix,
  4441. eb->start >> PAGE_CACHE_SHIFT);
  4442. spin_unlock(&fs_info->buffer_lock);
  4443. } else {
  4444. spin_unlock(&eb->refs_lock);
  4445. }
  4446. /* Should be safe to release our pages at this point */
  4447. btrfs_release_extent_buffer_page(eb);
  4448. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  4449. if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))) {
  4450. __free_extent_buffer(eb);
  4451. return 1;
  4452. }
  4453. #endif
  4454. call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
  4455. return 1;
  4456. }
  4457. spin_unlock(&eb->refs_lock);
  4458. return 0;
  4459. }
  4460. void free_extent_buffer(struct extent_buffer *eb)
  4461. {
  4462. int refs;
  4463. int old;
  4464. if (!eb)
  4465. return;
  4466. while (1) {
  4467. refs = atomic_read(&eb->refs);
  4468. if (refs <= 3)
  4469. break;
  4470. old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
  4471. if (old == refs)
  4472. return;
  4473. }
  4474. spin_lock(&eb->refs_lock);
  4475. if (atomic_read(&eb->refs) == 2 &&
  4476. test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
  4477. atomic_dec(&eb->refs);
  4478. if (atomic_read(&eb->refs) == 2 &&
  4479. test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
  4480. !extent_buffer_under_io(eb) &&
  4481. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4482. atomic_dec(&eb->refs);
  4483. /*
  4484. * I know this is terrible, but it's temporary until we stop tracking
  4485. * the uptodate bits and such for the extent buffers.
  4486. */
  4487. release_extent_buffer(eb);
  4488. }
  4489. void free_extent_buffer_stale(struct extent_buffer *eb)
  4490. {
  4491. if (!eb)
  4492. return;
  4493. spin_lock(&eb->refs_lock);
  4494. set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
  4495. if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
  4496. test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
  4497. atomic_dec(&eb->refs);
  4498. release_extent_buffer(eb);
  4499. }
  4500. void clear_extent_buffer_dirty(struct extent_buffer *eb)
  4501. {
  4502. unsigned long i;
  4503. unsigned long num_pages;
  4504. struct page *page;
  4505. num_pages = num_extent_pages(eb->start, eb->len);
  4506. for (i = 0; i < num_pages; i++) {
  4507. page = eb->pages[i];
  4508. if (!PageDirty(page))
  4509. continue;
  4510. lock_page(page);
  4511. WARN_ON(!PagePrivate(page));
  4512. clear_page_dirty_for_io(page);
  4513. spin_lock_irq(&page->mapping->tree_lock);
  4514. if (!PageDirty(page)) {
  4515. radix_tree_tag_clear(&page->mapping->page_tree,
  4516. page_index(page),
  4517. PAGECACHE_TAG_DIRTY);
  4518. }
  4519. spin_unlock_irq(&page->mapping->tree_lock);
  4520. ClearPageError(page);
  4521. unlock_page(page);
  4522. }
  4523. WARN_ON(atomic_read(&eb->refs) == 0);
  4524. }
  4525. int set_extent_buffer_dirty(struct extent_buffer *eb)
  4526. {
  4527. unsigned long i;
  4528. unsigned long num_pages;
  4529. int was_dirty = 0;
  4530. check_buffer_tree_ref(eb);
  4531. was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
  4532. num_pages = num_extent_pages(eb->start, eb->len);
  4533. WARN_ON(atomic_read(&eb->refs) == 0);
  4534. WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
  4535. for (i = 0; i < num_pages; i++)
  4536. set_page_dirty(eb->pages[i]);
  4537. return was_dirty;
  4538. }
  4539. int clear_extent_buffer_uptodate(struct extent_buffer *eb)
  4540. {
  4541. unsigned long i;
  4542. struct page *page;
  4543. unsigned long num_pages;
  4544. clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4545. num_pages = num_extent_pages(eb->start, eb->len);
  4546. for (i = 0; i < num_pages; i++) {
  4547. page = eb->pages[i];
  4548. if (page)
  4549. ClearPageUptodate(page);
  4550. }
  4551. return 0;
  4552. }
  4553. int set_extent_buffer_uptodate(struct extent_buffer *eb)
  4554. {
  4555. unsigned long i;
  4556. struct page *page;
  4557. unsigned long num_pages;
  4558. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4559. num_pages = num_extent_pages(eb->start, eb->len);
  4560. for (i = 0; i < num_pages; i++) {
  4561. page = eb->pages[i];
  4562. SetPageUptodate(page);
  4563. }
  4564. return 0;
  4565. }
  4566. int extent_buffer_uptodate(struct extent_buffer *eb)
  4567. {
  4568. return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4569. }
  4570. int read_extent_buffer_pages(struct extent_io_tree *tree,
  4571. struct extent_buffer *eb, u64 start, int wait,
  4572. get_extent_t *get_extent, int mirror_num)
  4573. {
  4574. unsigned long i;
  4575. unsigned long start_i;
  4576. struct page *page;
  4577. int err;
  4578. int ret = 0;
  4579. int locked_pages = 0;
  4580. int all_uptodate = 1;
  4581. unsigned long num_pages;
  4582. unsigned long num_reads = 0;
  4583. struct bio *bio = NULL;
  4584. unsigned long bio_flags = 0;
  4585. if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
  4586. return 0;
  4587. if (start) {
  4588. WARN_ON(start < eb->start);
  4589. start_i = (start >> PAGE_CACHE_SHIFT) -
  4590. (eb->start >> PAGE_CACHE_SHIFT);
  4591. } else {
  4592. start_i = 0;
  4593. }
  4594. num_pages = num_extent_pages(eb->start, eb->len);
  4595. for (i = start_i; i < num_pages; i++) {
  4596. page = eb->pages[i];
  4597. if (wait == WAIT_NONE) {
  4598. if (!trylock_page(page))
  4599. goto unlock_exit;
  4600. } else {
  4601. lock_page(page);
  4602. }
  4603. locked_pages++;
  4604. if (!PageUptodate(page)) {
  4605. num_reads++;
  4606. all_uptodate = 0;
  4607. }
  4608. }
  4609. if (all_uptodate) {
  4610. if (start_i == 0)
  4611. set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
  4612. goto unlock_exit;
  4613. }
  4614. clear_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  4615. eb->read_mirror = 0;
  4616. atomic_set(&eb->io_pages, num_reads);
  4617. for (i = start_i; i < num_pages; i++) {
  4618. page = eb->pages[i];
  4619. if (!PageUptodate(page)) {
  4620. ClearPageError(page);
  4621. err = __extent_read_full_page(tree, page,
  4622. get_extent, &bio,
  4623. mirror_num, &bio_flags,
  4624. READ | REQ_META);
  4625. if (err)
  4626. ret = err;
  4627. } else {
  4628. unlock_page(page);
  4629. }
  4630. }
  4631. if (bio) {
  4632. err = submit_one_bio(READ | REQ_META, bio, mirror_num,
  4633. bio_flags);
  4634. if (err)
  4635. return err;
  4636. }
  4637. if (ret || wait != WAIT_COMPLETE)
  4638. return ret;
  4639. for (i = start_i; i < num_pages; i++) {
  4640. page = eb->pages[i];
  4641. wait_on_page_locked(page);
  4642. if (!PageUptodate(page))
  4643. ret = -EIO;
  4644. }
  4645. return ret;
  4646. unlock_exit:
  4647. i = start_i;
  4648. while (locked_pages > 0) {
  4649. page = eb->pages[i];
  4650. i++;
  4651. unlock_page(page);
  4652. locked_pages--;
  4653. }
  4654. return ret;
  4655. }
  4656. void read_extent_buffer(struct extent_buffer *eb, void *dstv,
  4657. unsigned long start,
  4658. unsigned long len)
  4659. {
  4660. size_t cur;
  4661. size_t offset;
  4662. struct page *page;
  4663. char *kaddr;
  4664. char *dst = (char *)dstv;
  4665. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4666. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4667. WARN_ON(start > eb->len);
  4668. WARN_ON(start + len > eb->start + eb->len);
  4669. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4670. while (len > 0) {
  4671. page = eb->pages[i];
  4672. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4673. kaddr = page_address(page);
  4674. memcpy(dst, kaddr + offset, cur);
  4675. dst += cur;
  4676. len -= cur;
  4677. offset = 0;
  4678. i++;
  4679. }
  4680. }
  4681. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dstv,
  4682. unsigned long start,
  4683. unsigned long len)
  4684. {
  4685. size_t cur;
  4686. size_t offset;
  4687. struct page *page;
  4688. char *kaddr;
  4689. char __user *dst = (char __user *)dstv;
  4690. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4691. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4692. int ret = 0;
  4693. WARN_ON(start > eb->len);
  4694. WARN_ON(start + len > eb->start + eb->len);
  4695. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4696. while (len > 0) {
  4697. page = eb->pages[i];
  4698. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4699. kaddr = page_address(page);
  4700. if (copy_to_user(dst, kaddr + offset, cur)) {
  4701. ret = -EFAULT;
  4702. break;
  4703. }
  4704. dst += cur;
  4705. len -= cur;
  4706. offset = 0;
  4707. i++;
  4708. }
  4709. return ret;
  4710. }
  4711. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
  4712. unsigned long min_len, char **map,
  4713. unsigned long *map_start,
  4714. unsigned long *map_len)
  4715. {
  4716. size_t offset = start & (PAGE_CACHE_SIZE - 1);
  4717. char *kaddr;
  4718. struct page *p;
  4719. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4720. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4721. unsigned long end_i = (start_offset + start + min_len - 1) >>
  4722. PAGE_CACHE_SHIFT;
  4723. if (i != end_i)
  4724. return -EINVAL;
  4725. if (i == 0) {
  4726. offset = start_offset;
  4727. *map_start = 0;
  4728. } else {
  4729. offset = 0;
  4730. *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
  4731. }
  4732. if (start + min_len > eb->len) {
  4733. WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
  4734. "wanted %lu %lu\n",
  4735. eb->start, eb->len, start, min_len);
  4736. return -EINVAL;
  4737. }
  4738. p = eb->pages[i];
  4739. kaddr = page_address(p);
  4740. *map = kaddr + offset;
  4741. *map_len = PAGE_CACHE_SIZE - offset;
  4742. return 0;
  4743. }
  4744. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  4745. unsigned long start,
  4746. unsigned long len)
  4747. {
  4748. size_t cur;
  4749. size_t offset;
  4750. struct page *page;
  4751. char *kaddr;
  4752. char *ptr = (char *)ptrv;
  4753. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4754. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4755. int ret = 0;
  4756. WARN_ON(start > eb->len);
  4757. WARN_ON(start + len > eb->start + eb->len);
  4758. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4759. while (len > 0) {
  4760. page = eb->pages[i];
  4761. cur = min(len, (PAGE_CACHE_SIZE - offset));
  4762. kaddr = page_address(page);
  4763. ret = memcmp(ptr, kaddr + offset, cur);
  4764. if (ret)
  4765. break;
  4766. ptr += cur;
  4767. len -= cur;
  4768. offset = 0;
  4769. i++;
  4770. }
  4771. return ret;
  4772. }
  4773. void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
  4774. unsigned long start, unsigned long len)
  4775. {
  4776. size_t cur;
  4777. size_t offset;
  4778. struct page *page;
  4779. char *kaddr;
  4780. char *src = (char *)srcv;
  4781. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4782. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4783. WARN_ON(start > eb->len);
  4784. WARN_ON(start + len > eb->start + eb->len);
  4785. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4786. while (len > 0) {
  4787. page = eb->pages[i];
  4788. WARN_ON(!PageUptodate(page));
  4789. cur = min(len, PAGE_CACHE_SIZE - offset);
  4790. kaddr = page_address(page);
  4791. memcpy(kaddr + offset, src, cur);
  4792. src += cur;
  4793. len -= cur;
  4794. offset = 0;
  4795. i++;
  4796. }
  4797. }
  4798. void memset_extent_buffer(struct extent_buffer *eb, char c,
  4799. unsigned long start, unsigned long len)
  4800. {
  4801. size_t cur;
  4802. size_t offset;
  4803. struct page *page;
  4804. char *kaddr;
  4805. size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
  4806. unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
  4807. WARN_ON(start > eb->len);
  4808. WARN_ON(start + len > eb->start + eb->len);
  4809. offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
  4810. while (len > 0) {
  4811. page = eb->pages[i];
  4812. WARN_ON(!PageUptodate(page));
  4813. cur = min(len, PAGE_CACHE_SIZE - offset);
  4814. kaddr = page_address(page);
  4815. memset(kaddr + offset, c, cur);
  4816. len -= cur;
  4817. offset = 0;
  4818. i++;
  4819. }
  4820. }
  4821. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  4822. unsigned long dst_offset, unsigned long src_offset,
  4823. unsigned long len)
  4824. {
  4825. u64 dst_len = dst->len;
  4826. size_t cur;
  4827. size_t offset;
  4828. struct page *page;
  4829. char *kaddr;
  4830. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4831. unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4832. WARN_ON(src->len != dst_len);
  4833. offset = (start_offset + dst_offset) &
  4834. (PAGE_CACHE_SIZE - 1);
  4835. while (len > 0) {
  4836. page = dst->pages[i];
  4837. WARN_ON(!PageUptodate(page));
  4838. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
  4839. kaddr = page_address(page);
  4840. read_extent_buffer(src, kaddr + offset, src_offset, cur);
  4841. src_offset += cur;
  4842. len -= cur;
  4843. offset = 0;
  4844. i++;
  4845. }
  4846. }
  4847. static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
  4848. {
  4849. unsigned long distance = (src > dst) ? src - dst : dst - src;
  4850. return distance < len;
  4851. }
  4852. static void copy_pages(struct page *dst_page, struct page *src_page,
  4853. unsigned long dst_off, unsigned long src_off,
  4854. unsigned long len)
  4855. {
  4856. char *dst_kaddr = page_address(dst_page);
  4857. char *src_kaddr;
  4858. int must_memmove = 0;
  4859. if (dst_page != src_page) {
  4860. src_kaddr = page_address(src_page);
  4861. } else {
  4862. src_kaddr = dst_kaddr;
  4863. if (areas_overlap(src_off, dst_off, len))
  4864. must_memmove = 1;
  4865. }
  4866. if (must_memmove)
  4867. memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4868. else
  4869. memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
  4870. }
  4871. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4872. unsigned long src_offset, unsigned long len)
  4873. {
  4874. size_t cur;
  4875. size_t dst_off_in_page;
  4876. size_t src_off_in_page;
  4877. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4878. unsigned long dst_i;
  4879. unsigned long src_i;
  4880. if (src_offset + len > dst->len) {
  4881. printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move "
  4882. "len %lu dst len %lu\n", src_offset, len, dst->len);
  4883. BUG_ON(1);
  4884. }
  4885. if (dst_offset + len > dst->len) {
  4886. printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move "
  4887. "len %lu dst len %lu\n", dst_offset, len, dst->len);
  4888. BUG_ON(1);
  4889. }
  4890. while (len > 0) {
  4891. dst_off_in_page = (start_offset + dst_offset) &
  4892. (PAGE_CACHE_SIZE - 1);
  4893. src_off_in_page = (start_offset + src_offset) &
  4894. (PAGE_CACHE_SIZE - 1);
  4895. dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
  4896. src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
  4897. cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
  4898. src_off_in_page));
  4899. cur = min_t(unsigned long, cur,
  4900. (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
  4901. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  4902. dst_off_in_page, src_off_in_page, cur);
  4903. src_offset += cur;
  4904. dst_offset += cur;
  4905. len -= cur;
  4906. }
  4907. }
  4908. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  4909. unsigned long src_offset, unsigned long len)
  4910. {
  4911. size_t cur;
  4912. size_t dst_off_in_page;
  4913. size_t src_off_in_page;
  4914. unsigned long dst_end = dst_offset + len - 1;
  4915. unsigned long src_end = src_offset + len - 1;
  4916. size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
  4917. unsigned long dst_i;
  4918. unsigned long src_i;
  4919. if (src_offset + len > dst->len) {
  4920. printk(KERN_ERR "BTRFS: memmove bogus src_offset %lu move "
  4921. "len %lu len %lu\n", src_offset, len, dst->len);
  4922. BUG_ON(1);
  4923. }
  4924. if (dst_offset + len > dst->len) {
  4925. printk(KERN_ERR "BTRFS: memmove bogus dst_offset %lu move "
  4926. "len %lu len %lu\n", dst_offset, len, dst->len);
  4927. BUG_ON(1);
  4928. }
  4929. if (dst_offset < src_offset) {
  4930. memcpy_extent_buffer(dst, dst_offset, src_offset, len);
  4931. return;
  4932. }
  4933. while (len > 0) {
  4934. dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
  4935. src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
  4936. dst_off_in_page = (start_offset + dst_end) &
  4937. (PAGE_CACHE_SIZE - 1);
  4938. src_off_in_page = (start_offset + src_end) &
  4939. (PAGE_CACHE_SIZE - 1);
  4940. cur = min_t(unsigned long, len, src_off_in_page + 1);
  4941. cur = min(cur, dst_off_in_page + 1);
  4942. copy_pages(dst->pages[dst_i], dst->pages[src_i],
  4943. dst_off_in_page - cur + 1,
  4944. src_off_in_page - cur + 1, cur);
  4945. dst_end -= cur;
  4946. src_end -= cur;
  4947. len -= cur;
  4948. }
  4949. }
  4950. int try_release_extent_buffer(struct page *page)
  4951. {
  4952. struct extent_buffer *eb;
  4953. /*
  4954. * We need to make sure noboody is attaching this page to an eb right
  4955. * now.
  4956. */
  4957. spin_lock(&page->mapping->private_lock);
  4958. if (!PagePrivate(page)) {
  4959. spin_unlock(&page->mapping->private_lock);
  4960. return 1;
  4961. }
  4962. eb = (struct extent_buffer *)page->private;
  4963. BUG_ON(!eb);
  4964. /*
  4965. * This is a little awful but should be ok, we need to make sure that
  4966. * the eb doesn't disappear out from under us while we're looking at
  4967. * this page.
  4968. */
  4969. spin_lock(&eb->refs_lock);
  4970. if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
  4971. spin_unlock(&eb->refs_lock);
  4972. spin_unlock(&page->mapping->private_lock);
  4973. return 0;
  4974. }
  4975. spin_unlock(&page->mapping->private_lock);
  4976. /*
  4977. * If tree ref isn't set then we know the ref on this eb is a real ref,
  4978. * so just return, this page will likely be freed soon anyway.
  4979. */
  4980. if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
  4981. spin_unlock(&eb->refs_lock);
  4982. return 0;
  4983. }
  4984. return release_extent_buffer(eb);
  4985. }