extent_io.c 135 KB

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