extent_io.c 136 KB

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