inode.c 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080
  1. /*
  2. * linux/fs/ext4/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  16. * (jj@sunsite.ms.mff.cuni.cz)
  17. *
  18. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  19. */
  20. #include <linux/fs.h>
  21. #include <linux/time.h>
  22. #include <linux/jbd2.h>
  23. #include <linux/highuid.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/quotaops.h>
  26. #include <linux/string.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/writeback.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/mpage.h>
  31. #include <linux/namei.h>
  32. #include <linux/uio.h>
  33. #include <linux/bio.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/kernel.h>
  36. #include <linux/printk.h>
  37. #include <linux/slab.h>
  38. #include <linux/ratelimit.h>
  39. #include <linux/aio.h>
  40. #include "ext4_jbd2.h"
  41. #include "xattr.h"
  42. #include "acl.h"
  43. #include "truncate.h"
  44. #include <trace/events/ext4.h>
  45. #define MPAGE_DA_EXTENT_TAIL 0x01
  46. static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
  47. struct ext4_inode_info *ei)
  48. {
  49. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  50. __u16 csum_lo;
  51. __u16 csum_hi = 0;
  52. __u32 csum;
  53. csum_lo = le16_to_cpu(raw->i_checksum_lo);
  54. raw->i_checksum_lo = 0;
  55. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  56. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
  57. csum_hi = le16_to_cpu(raw->i_checksum_hi);
  58. raw->i_checksum_hi = 0;
  59. }
  60. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
  61. EXT4_INODE_SIZE(inode->i_sb));
  62. raw->i_checksum_lo = cpu_to_le16(csum_lo);
  63. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  64. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  65. raw->i_checksum_hi = cpu_to_le16(csum_hi);
  66. return csum;
  67. }
  68. static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
  69. struct ext4_inode_info *ei)
  70. {
  71. __u32 provided, calculated;
  72. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  73. cpu_to_le32(EXT4_OS_LINUX) ||
  74. !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  75. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  76. return 1;
  77. provided = le16_to_cpu(raw->i_checksum_lo);
  78. calculated = ext4_inode_csum(inode, raw, ei);
  79. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  80. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  81. provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
  82. else
  83. calculated &= 0xFFFF;
  84. return provided == calculated;
  85. }
  86. static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
  87. struct ext4_inode_info *ei)
  88. {
  89. __u32 csum;
  90. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  91. cpu_to_le32(EXT4_OS_LINUX) ||
  92. !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  93. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  94. return;
  95. csum = ext4_inode_csum(inode, raw, ei);
  96. raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
  97. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  98. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  99. raw->i_checksum_hi = cpu_to_le16(csum >> 16);
  100. }
  101. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  102. loff_t new_size)
  103. {
  104. trace_ext4_begin_ordered_truncate(inode, new_size);
  105. /*
  106. * If jinode is zero, then we never opened the file for
  107. * writing, so there's no need to call
  108. * jbd2_journal_begin_ordered_truncate() since there's no
  109. * outstanding writes we need to flush.
  110. */
  111. if (!EXT4_I(inode)->jinode)
  112. return 0;
  113. return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
  114. EXT4_I(inode)->jinode,
  115. new_size);
  116. }
  117. static void ext4_invalidatepage(struct page *page, unsigned int offset,
  118. unsigned int length);
  119. static int __ext4_journalled_writepage(struct page *page, unsigned int len);
  120. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
  121. static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
  122. int pextents);
  123. /*
  124. * Test whether an inode is a fast symlink.
  125. */
  126. static int ext4_inode_is_fast_symlink(struct inode *inode)
  127. {
  128. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  129. (inode->i_sb->s_blocksize >> 9) : 0;
  130. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  131. }
  132. /*
  133. * Restart the transaction associated with *handle. This does a commit,
  134. * so before we call here everything must be consistently dirtied against
  135. * this transaction.
  136. */
  137. int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
  138. int nblocks)
  139. {
  140. int ret;
  141. /*
  142. * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
  143. * moment, get_block can be called only for blocks inside i_size since
  144. * page cache has been already dropped and writes are blocked by
  145. * i_mutex. So we can safely drop the i_data_sem here.
  146. */
  147. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  148. jbd_debug(2, "restarting handle %p\n", handle);
  149. up_write(&EXT4_I(inode)->i_data_sem);
  150. ret = ext4_journal_restart(handle, nblocks);
  151. down_write(&EXT4_I(inode)->i_data_sem);
  152. ext4_discard_preallocations(inode);
  153. return ret;
  154. }
  155. /*
  156. * Called at the last iput() if i_nlink is zero.
  157. */
  158. void ext4_evict_inode(struct inode *inode)
  159. {
  160. handle_t *handle;
  161. int err;
  162. trace_ext4_evict_inode(inode);
  163. if (inode->i_nlink) {
  164. /*
  165. * When journalling data dirty buffers are tracked only in the
  166. * journal. So although mm thinks everything is clean and
  167. * ready for reaping the inode might still have some pages to
  168. * write in the running transaction or waiting to be
  169. * checkpointed. Thus calling jbd2_journal_invalidatepage()
  170. * (via truncate_inode_pages()) to discard these buffers can
  171. * cause data loss. Also even if we did not discard these
  172. * buffers, we would have no way to find them after the inode
  173. * is reaped and thus user could see stale data if he tries to
  174. * read them before the transaction is checkpointed. So be
  175. * careful and force everything to disk here... We use
  176. * ei->i_datasync_tid to store the newest transaction
  177. * containing inode's data.
  178. *
  179. * Note that directories do not have this problem because they
  180. * don't use page cache.
  181. */
  182. if (ext4_should_journal_data(inode) &&
  183. (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
  184. inode->i_ino != EXT4_JOURNAL_INO) {
  185. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  186. tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
  187. jbd2_complete_transaction(journal, commit_tid);
  188. filemap_write_and_wait(&inode->i_data);
  189. }
  190. truncate_inode_pages(&inode->i_data, 0);
  191. ext4_ioend_shutdown(inode);
  192. goto no_delete;
  193. }
  194. if (!is_bad_inode(inode))
  195. dquot_initialize(inode);
  196. if (ext4_should_order_data(inode))
  197. ext4_begin_ordered_truncate(inode, 0);
  198. truncate_inode_pages(&inode->i_data, 0);
  199. ext4_ioend_shutdown(inode);
  200. if (is_bad_inode(inode))
  201. goto no_delete;
  202. /*
  203. * Protect us against freezing - iput() caller didn't have to have any
  204. * protection against it
  205. */
  206. sb_start_intwrite(inode->i_sb);
  207. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
  208. ext4_blocks_for_truncate(inode)+3);
  209. if (IS_ERR(handle)) {
  210. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  211. /*
  212. * If we're going to skip the normal cleanup, we still need to
  213. * make sure that the in-core orphan linked list is properly
  214. * cleaned up.
  215. */
  216. ext4_orphan_del(NULL, inode);
  217. sb_end_intwrite(inode->i_sb);
  218. goto no_delete;
  219. }
  220. if (IS_SYNC(inode))
  221. ext4_handle_sync(handle);
  222. inode->i_size = 0;
  223. err = ext4_mark_inode_dirty(handle, inode);
  224. if (err) {
  225. ext4_warning(inode->i_sb,
  226. "couldn't mark inode dirty (err %d)", err);
  227. goto stop_handle;
  228. }
  229. if (inode->i_blocks)
  230. ext4_truncate(inode);
  231. /*
  232. * ext4_ext_truncate() doesn't reserve any slop when it
  233. * restarts journal transactions; therefore there may not be
  234. * enough credits left in the handle to remove the inode from
  235. * the orphan list and set the dtime field.
  236. */
  237. if (!ext4_handle_has_enough_credits(handle, 3)) {
  238. err = ext4_journal_extend(handle, 3);
  239. if (err > 0)
  240. err = ext4_journal_restart(handle, 3);
  241. if (err != 0) {
  242. ext4_warning(inode->i_sb,
  243. "couldn't extend journal (err %d)", err);
  244. stop_handle:
  245. ext4_journal_stop(handle);
  246. ext4_orphan_del(NULL, inode);
  247. sb_end_intwrite(inode->i_sb);
  248. goto no_delete;
  249. }
  250. }
  251. /*
  252. * Kill off the orphan record which ext4_truncate created.
  253. * AKPM: I think this can be inside the above `if'.
  254. * Note that ext4_orphan_del() has to be able to cope with the
  255. * deletion of a non-existent orphan - this is because we don't
  256. * know if ext4_truncate() actually created an orphan record.
  257. * (Well, we could do this if we need to, but heck - it works)
  258. */
  259. ext4_orphan_del(handle, inode);
  260. EXT4_I(inode)->i_dtime = get_seconds();
  261. /*
  262. * One subtle ordering requirement: if anything has gone wrong
  263. * (transaction abort, IO errors, whatever), then we can still
  264. * do these next steps (the fs will already have been marked as
  265. * having errors), but we can't free the inode if the mark_dirty
  266. * fails.
  267. */
  268. if (ext4_mark_inode_dirty(handle, inode))
  269. /* If that failed, just do the required in-core inode clear. */
  270. ext4_clear_inode(inode);
  271. else
  272. ext4_free_inode(handle, inode);
  273. ext4_journal_stop(handle);
  274. sb_end_intwrite(inode->i_sb);
  275. return;
  276. no_delete:
  277. ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
  278. }
  279. #ifdef CONFIG_QUOTA
  280. qsize_t *ext4_get_reserved_space(struct inode *inode)
  281. {
  282. return &EXT4_I(inode)->i_reserved_quota;
  283. }
  284. #endif
  285. /*
  286. * Calculate the number of metadata blocks need to reserve
  287. * to allocate a block located at @lblock
  288. */
  289. static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  290. {
  291. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  292. return ext4_ext_calc_metadata_amount(inode, lblock);
  293. return ext4_ind_calc_metadata_amount(inode, lblock);
  294. }
  295. /*
  296. * Called with i_data_sem down, which is important since we can call
  297. * ext4_discard_preallocations() from here.
  298. */
  299. void ext4_da_update_reserve_space(struct inode *inode,
  300. int used, int quota_claim)
  301. {
  302. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  303. struct ext4_inode_info *ei = EXT4_I(inode);
  304. spin_lock(&ei->i_block_reservation_lock);
  305. trace_ext4_da_update_reserve_space(inode, used, quota_claim);
  306. if (unlikely(used > ei->i_reserved_data_blocks)) {
  307. ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
  308. "with only %d reserved data blocks",
  309. __func__, inode->i_ino, used,
  310. ei->i_reserved_data_blocks);
  311. WARN_ON(1);
  312. used = ei->i_reserved_data_blocks;
  313. }
  314. if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
  315. ext4_warning(inode->i_sb, "ino %lu, allocated %d "
  316. "with only %d reserved metadata blocks "
  317. "(releasing %d blocks with reserved %d data blocks)",
  318. inode->i_ino, ei->i_allocated_meta_blocks,
  319. ei->i_reserved_meta_blocks, used,
  320. ei->i_reserved_data_blocks);
  321. WARN_ON(1);
  322. ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
  323. }
  324. /* Update per-inode reservations */
  325. ei->i_reserved_data_blocks -= used;
  326. ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
  327. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  328. used + ei->i_allocated_meta_blocks);
  329. ei->i_allocated_meta_blocks = 0;
  330. if (ei->i_reserved_data_blocks == 0) {
  331. /*
  332. * We can release all of the reserved metadata blocks
  333. * only when we have written all of the delayed
  334. * allocation blocks.
  335. */
  336. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  337. ei->i_reserved_meta_blocks);
  338. ei->i_reserved_meta_blocks = 0;
  339. ei->i_da_metadata_calc_len = 0;
  340. }
  341. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  342. /* Update quota subsystem for data blocks */
  343. if (quota_claim)
  344. dquot_claim_block(inode, EXT4_C2B(sbi, used));
  345. else {
  346. /*
  347. * We did fallocate with an offset that is already delayed
  348. * allocated. So on delayed allocated writeback we should
  349. * not re-claim the quota for fallocated blocks.
  350. */
  351. dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
  352. }
  353. /*
  354. * If we have done all the pending block allocations and if
  355. * there aren't any writers on the inode, we can discard the
  356. * inode's preallocations.
  357. */
  358. if ((ei->i_reserved_data_blocks == 0) &&
  359. (atomic_read(&inode->i_writecount) == 0))
  360. ext4_discard_preallocations(inode);
  361. }
  362. static int __check_block_validity(struct inode *inode, const char *func,
  363. unsigned int line,
  364. struct ext4_map_blocks *map)
  365. {
  366. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
  367. map->m_len)) {
  368. ext4_error_inode(inode, func, line, map->m_pblk,
  369. "lblock %lu mapped to illegal pblock "
  370. "(length %d)", (unsigned long) map->m_lblk,
  371. map->m_len);
  372. return -EIO;
  373. }
  374. return 0;
  375. }
  376. #define check_block_validity(inode, map) \
  377. __check_block_validity((inode), __func__, __LINE__, (map))
  378. #ifdef ES_AGGRESSIVE_TEST
  379. static void ext4_map_blocks_es_recheck(handle_t *handle,
  380. struct inode *inode,
  381. struct ext4_map_blocks *es_map,
  382. struct ext4_map_blocks *map,
  383. int flags)
  384. {
  385. int retval;
  386. map->m_flags = 0;
  387. /*
  388. * There is a race window that the result is not the same.
  389. * e.g. xfstests #223 when dioread_nolock enables. The reason
  390. * is that we lookup a block mapping in extent status tree with
  391. * out taking i_data_sem. So at the time the unwritten extent
  392. * could be converted.
  393. */
  394. if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
  395. down_read((&EXT4_I(inode)->i_data_sem));
  396. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  397. retval = ext4_ext_map_blocks(handle, inode, map, flags &
  398. EXT4_GET_BLOCKS_KEEP_SIZE);
  399. } else {
  400. retval = ext4_ind_map_blocks(handle, inode, map, flags &
  401. EXT4_GET_BLOCKS_KEEP_SIZE);
  402. }
  403. if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
  404. up_read((&EXT4_I(inode)->i_data_sem));
  405. /*
  406. * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag
  407. * because it shouldn't be marked in es_map->m_flags.
  408. */
  409. map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY);
  410. /*
  411. * We don't check m_len because extent will be collpased in status
  412. * tree. So the m_len might not equal.
  413. */
  414. if (es_map->m_lblk != map->m_lblk ||
  415. es_map->m_flags != map->m_flags ||
  416. es_map->m_pblk != map->m_pblk) {
  417. printk("ES cache assertation failed for inode: %lu "
  418. "es_cached ex [%d/%d/%llu/%x] != "
  419. "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
  420. inode->i_ino, es_map->m_lblk, es_map->m_len,
  421. es_map->m_pblk, es_map->m_flags, map->m_lblk,
  422. map->m_len, map->m_pblk, map->m_flags,
  423. retval, flags);
  424. }
  425. }
  426. #endif /* ES_AGGRESSIVE_TEST */
  427. /*
  428. * The ext4_map_blocks() function tries to look up the requested blocks,
  429. * and returns if the blocks are already mapped.
  430. *
  431. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  432. * and store the allocated blocks in the result buffer head and mark it
  433. * mapped.
  434. *
  435. * If file type is extents based, it will call ext4_ext_map_blocks(),
  436. * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  437. * based files
  438. *
  439. * On success, it returns the number of blocks being mapped or allocate.
  440. * if create==0 and the blocks are pre-allocated and uninitialized block,
  441. * the result buffer head is unmapped. If the create ==1, it will make sure
  442. * the buffer head is mapped.
  443. *
  444. * It returns 0 if plain look up failed (blocks have not been allocated), in
  445. * that case, buffer head is unmapped
  446. *
  447. * It returns the error in case of allocation failure.
  448. */
  449. int ext4_map_blocks(handle_t *handle, struct inode *inode,
  450. struct ext4_map_blocks *map, int flags)
  451. {
  452. struct extent_status es;
  453. int retval;
  454. #ifdef ES_AGGRESSIVE_TEST
  455. struct ext4_map_blocks orig_map;
  456. memcpy(&orig_map, map, sizeof(*map));
  457. #endif
  458. map->m_flags = 0;
  459. ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
  460. "logical block %lu\n", inode->i_ino, flags, map->m_len,
  461. (unsigned long) map->m_lblk);
  462. /* Lookup extent status tree firstly */
  463. if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
  464. if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
  465. map->m_pblk = ext4_es_pblock(&es) +
  466. map->m_lblk - es.es_lblk;
  467. map->m_flags |= ext4_es_is_written(&es) ?
  468. EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
  469. retval = es.es_len - (map->m_lblk - es.es_lblk);
  470. if (retval > map->m_len)
  471. retval = map->m_len;
  472. map->m_len = retval;
  473. } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
  474. retval = 0;
  475. } else {
  476. BUG_ON(1);
  477. }
  478. #ifdef ES_AGGRESSIVE_TEST
  479. ext4_map_blocks_es_recheck(handle, inode, map,
  480. &orig_map, flags);
  481. #endif
  482. goto found;
  483. }
  484. /*
  485. * Try to see if we can get the block without requesting a new
  486. * file system block.
  487. */
  488. if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
  489. down_read((&EXT4_I(inode)->i_data_sem));
  490. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  491. retval = ext4_ext_map_blocks(handle, inode, map, flags &
  492. EXT4_GET_BLOCKS_KEEP_SIZE);
  493. } else {
  494. retval = ext4_ind_map_blocks(handle, inode, map, flags &
  495. EXT4_GET_BLOCKS_KEEP_SIZE);
  496. }
  497. if (retval > 0) {
  498. int ret;
  499. unsigned long long status;
  500. #ifdef ES_AGGRESSIVE_TEST
  501. if (retval != map->m_len) {
  502. printk("ES len assertation failed for inode: %lu "
  503. "retval %d != map->m_len %d "
  504. "in %s (lookup)\n", inode->i_ino, retval,
  505. map->m_len, __func__);
  506. }
  507. #endif
  508. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  509. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  510. if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
  511. ext4_find_delalloc_range(inode, map->m_lblk,
  512. map->m_lblk + map->m_len - 1))
  513. status |= EXTENT_STATUS_DELAYED;
  514. ret = ext4_es_insert_extent(inode, map->m_lblk,
  515. map->m_len, map->m_pblk, status);
  516. if (ret < 0)
  517. retval = ret;
  518. }
  519. if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
  520. up_read((&EXT4_I(inode)->i_data_sem));
  521. found:
  522. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  523. int ret = check_block_validity(inode, map);
  524. if (ret != 0)
  525. return ret;
  526. }
  527. /* If it is only a block(s) look up */
  528. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  529. return retval;
  530. /*
  531. * Returns if the blocks have already allocated
  532. *
  533. * Note that if blocks have been preallocated
  534. * ext4_ext_get_block() returns the create = 0
  535. * with buffer head unmapped.
  536. */
  537. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
  538. return retval;
  539. /*
  540. * Here we clear m_flags because after allocating an new extent,
  541. * it will be set again.
  542. */
  543. map->m_flags &= ~EXT4_MAP_FLAGS;
  544. /*
  545. * New blocks allocate and/or writing to uninitialized extent
  546. * will possibly result in updating i_data, so we take
  547. * the write lock of i_data_sem, and call get_blocks()
  548. * with create == 1 flag.
  549. */
  550. down_write((&EXT4_I(inode)->i_data_sem));
  551. /*
  552. * if the caller is from delayed allocation writeout path
  553. * we have already reserved fs blocks for allocation
  554. * let the underlying get_block() function know to
  555. * avoid double accounting
  556. */
  557. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  558. ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
  559. /*
  560. * We need to check for EXT4 here because migrate
  561. * could have changed the inode type in between
  562. */
  563. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  564. retval = ext4_ext_map_blocks(handle, inode, map, flags);
  565. } else {
  566. retval = ext4_ind_map_blocks(handle, inode, map, flags);
  567. if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
  568. /*
  569. * We allocated new blocks which will result in
  570. * i_data's format changing. Force the migrate
  571. * to fail by clearing migrate flags
  572. */
  573. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  574. }
  575. /*
  576. * Update reserved blocks/metadata blocks after successful
  577. * block allocation which had been deferred till now. We don't
  578. * support fallocate for non extent files. So we can update
  579. * reserve space here.
  580. */
  581. if ((retval > 0) &&
  582. (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
  583. ext4_da_update_reserve_space(inode, retval, 1);
  584. }
  585. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  586. ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
  587. if (retval > 0) {
  588. int ret;
  589. unsigned long long status;
  590. #ifdef ES_AGGRESSIVE_TEST
  591. if (retval != map->m_len) {
  592. printk("ES len assertation failed for inode: %lu "
  593. "retval %d != map->m_len %d "
  594. "in %s (allocation)\n", inode->i_ino, retval,
  595. map->m_len, __func__);
  596. }
  597. #endif
  598. /*
  599. * If the extent has been zeroed out, we don't need to update
  600. * extent status tree.
  601. */
  602. if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
  603. ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
  604. if (ext4_es_is_written(&es))
  605. goto has_zeroout;
  606. }
  607. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  608. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  609. if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
  610. ext4_find_delalloc_range(inode, map->m_lblk,
  611. map->m_lblk + map->m_len - 1))
  612. status |= EXTENT_STATUS_DELAYED;
  613. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  614. map->m_pblk, status);
  615. if (ret < 0)
  616. retval = ret;
  617. }
  618. has_zeroout:
  619. up_write((&EXT4_I(inode)->i_data_sem));
  620. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  621. int ret = check_block_validity(inode, map);
  622. if (ret != 0)
  623. return ret;
  624. }
  625. return retval;
  626. }
  627. /* Maximum number of blocks we map for direct IO at once. */
  628. #define DIO_MAX_BLOCKS 4096
  629. static int _ext4_get_block(struct inode *inode, sector_t iblock,
  630. struct buffer_head *bh, int flags)
  631. {
  632. handle_t *handle = ext4_journal_current_handle();
  633. struct ext4_map_blocks map;
  634. int ret = 0, started = 0;
  635. int dio_credits;
  636. if (ext4_has_inline_data(inode))
  637. return -ERANGE;
  638. map.m_lblk = iblock;
  639. map.m_len = bh->b_size >> inode->i_blkbits;
  640. if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
  641. /* Direct IO write... */
  642. if (map.m_len > DIO_MAX_BLOCKS)
  643. map.m_len = DIO_MAX_BLOCKS;
  644. dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
  645. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  646. dio_credits);
  647. if (IS_ERR(handle)) {
  648. ret = PTR_ERR(handle);
  649. return ret;
  650. }
  651. started = 1;
  652. }
  653. ret = ext4_map_blocks(handle, inode, &map, flags);
  654. if (ret > 0) {
  655. map_bh(bh, inode->i_sb, map.m_pblk);
  656. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  657. bh->b_size = inode->i_sb->s_blocksize * map.m_len;
  658. ret = 0;
  659. }
  660. if (started)
  661. ext4_journal_stop(handle);
  662. return ret;
  663. }
  664. int ext4_get_block(struct inode *inode, sector_t iblock,
  665. struct buffer_head *bh, int create)
  666. {
  667. return _ext4_get_block(inode, iblock, bh,
  668. create ? EXT4_GET_BLOCKS_CREATE : 0);
  669. }
  670. /*
  671. * `handle' can be NULL if create is zero
  672. */
  673. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  674. ext4_lblk_t block, int create, int *errp)
  675. {
  676. struct ext4_map_blocks map;
  677. struct buffer_head *bh;
  678. int fatal = 0, err;
  679. J_ASSERT(handle != NULL || create == 0);
  680. map.m_lblk = block;
  681. map.m_len = 1;
  682. err = ext4_map_blocks(handle, inode, &map,
  683. create ? EXT4_GET_BLOCKS_CREATE : 0);
  684. /* ensure we send some value back into *errp */
  685. *errp = 0;
  686. if (create && err == 0)
  687. err = -ENOSPC; /* should never happen */
  688. if (err < 0)
  689. *errp = err;
  690. if (err <= 0)
  691. return NULL;
  692. bh = sb_getblk(inode->i_sb, map.m_pblk);
  693. if (unlikely(!bh)) {
  694. *errp = -ENOMEM;
  695. return NULL;
  696. }
  697. if (map.m_flags & EXT4_MAP_NEW) {
  698. J_ASSERT(create != 0);
  699. J_ASSERT(handle != NULL);
  700. /*
  701. * Now that we do not always journal data, we should
  702. * keep in mind whether this should always journal the
  703. * new buffer as metadata. For now, regular file
  704. * writes use ext4_get_block instead, so it's not a
  705. * problem.
  706. */
  707. lock_buffer(bh);
  708. BUFFER_TRACE(bh, "call get_create_access");
  709. fatal = ext4_journal_get_create_access(handle, bh);
  710. if (!fatal && !buffer_uptodate(bh)) {
  711. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  712. set_buffer_uptodate(bh);
  713. }
  714. unlock_buffer(bh);
  715. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  716. err = ext4_handle_dirty_metadata(handle, inode, bh);
  717. if (!fatal)
  718. fatal = err;
  719. } else {
  720. BUFFER_TRACE(bh, "not a new buffer");
  721. }
  722. if (fatal) {
  723. *errp = fatal;
  724. brelse(bh);
  725. bh = NULL;
  726. }
  727. return bh;
  728. }
  729. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  730. ext4_lblk_t block, int create, int *err)
  731. {
  732. struct buffer_head *bh;
  733. bh = ext4_getblk(handle, inode, block, create, err);
  734. if (!bh)
  735. return bh;
  736. if (buffer_uptodate(bh))
  737. return bh;
  738. ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
  739. wait_on_buffer(bh);
  740. if (buffer_uptodate(bh))
  741. return bh;
  742. put_bh(bh);
  743. *err = -EIO;
  744. return NULL;
  745. }
  746. int ext4_walk_page_buffers(handle_t *handle,
  747. struct buffer_head *head,
  748. unsigned from,
  749. unsigned to,
  750. int *partial,
  751. int (*fn)(handle_t *handle,
  752. struct buffer_head *bh))
  753. {
  754. struct buffer_head *bh;
  755. unsigned block_start, block_end;
  756. unsigned blocksize = head->b_size;
  757. int err, ret = 0;
  758. struct buffer_head *next;
  759. for (bh = head, block_start = 0;
  760. ret == 0 && (bh != head || !block_start);
  761. block_start = block_end, bh = next) {
  762. next = bh->b_this_page;
  763. block_end = block_start + blocksize;
  764. if (block_end <= from || block_start >= to) {
  765. if (partial && !buffer_uptodate(bh))
  766. *partial = 1;
  767. continue;
  768. }
  769. err = (*fn)(handle, bh);
  770. if (!ret)
  771. ret = err;
  772. }
  773. return ret;
  774. }
  775. /*
  776. * To preserve ordering, it is essential that the hole instantiation and
  777. * the data write be encapsulated in a single transaction. We cannot
  778. * close off a transaction and start a new one between the ext4_get_block()
  779. * and the commit_write(). So doing the jbd2_journal_start at the start of
  780. * prepare_write() is the right place.
  781. *
  782. * Also, this function can nest inside ext4_writepage(). In that case, we
  783. * *know* that ext4_writepage() has generated enough buffer credits to do the
  784. * whole page. So we won't block on the journal in that case, which is good,
  785. * because the caller may be PF_MEMALLOC.
  786. *
  787. * By accident, ext4 can be reentered when a transaction is open via
  788. * quota file writes. If we were to commit the transaction while thus
  789. * reentered, there can be a deadlock - we would be holding a quota
  790. * lock, and the commit would never complete if another thread had a
  791. * transaction open and was blocking on the quota lock - a ranking
  792. * violation.
  793. *
  794. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  795. * will _not_ run commit under these circumstances because handle->h_ref
  796. * is elevated. We'll still have enough credits for the tiny quotafile
  797. * write.
  798. */
  799. int do_journal_get_write_access(handle_t *handle,
  800. struct buffer_head *bh)
  801. {
  802. int dirty = buffer_dirty(bh);
  803. int ret;
  804. if (!buffer_mapped(bh) || buffer_freed(bh))
  805. return 0;
  806. /*
  807. * __block_write_begin() could have dirtied some buffers. Clean
  808. * the dirty bit as jbd2_journal_get_write_access() could complain
  809. * otherwise about fs integrity issues. Setting of the dirty bit
  810. * by __block_write_begin() isn't a real problem here as we clear
  811. * the bit before releasing a page lock and thus writeback cannot
  812. * ever write the buffer.
  813. */
  814. if (dirty)
  815. clear_buffer_dirty(bh);
  816. ret = ext4_journal_get_write_access(handle, bh);
  817. if (!ret && dirty)
  818. ret = ext4_handle_dirty_metadata(handle, NULL, bh);
  819. return ret;
  820. }
  821. static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
  822. struct buffer_head *bh_result, int create);
  823. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  824. loff_t pos, unsigned len, unsigned flags,
  825. struct page **pagep, void **fsdata)
  826. {
  827. struct inode *inode = mapping->host;
  828. int ret, needed_blocks;
  829. handle_t *handle;
  830. int retries = 0;
  831. struct page *page;
  832. pgoff_t index;
  833. unsigned from, to;
  834. trace_ext4_write_begin(inode, pos, len, flags);
  835. /*
  836. * Reserve one block more for addition to orphan list in case
  837. * we allocate blocks but write fails for some reason
  838. */
  839. needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
  840. index = pos >> PAGE_CACHE_SHIFT;
  841. from = pos & (PAGE_CACHE_SIZE - 1);
  842. to = from + len;
  843. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  844. ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
  845. flags, pagep);
  846. if (ret < 0)
  847. return ret;
  848. if (ret == 1)
  849. return 0;
  850. }
  851. /*
  852. * grab_cache_page_write_begin() can take a long time if the
  853. * system is thrashing due to memory pressure, or if the page
  854. * is being written back. So grab it first before we start
  855. * the transaction handle. This also allows us to allocate
  856. * the page (if needed) without using GFP_NOFS.
  857. */
  858. retry_grab:
  859. page = grab_cache_page_write_begin(mapping, index, flags);
  860. if (!page)
  861. return -ENOMEM;
  862. unlock_page(page);
  863. retry_journal:
  864. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  865. if (IS_ERR(handle)) {
  866. page_cache_release(page);
  867. return PTR_ERR(handle);
  868. }
  869. lock_page(page);
  870. if (page->mapping != mapping) {
  871. /* The page got truncated from under us */
  872. unlock_page(page);
  873. page_cache_release(page);
  874. ext4_journal_stop(handle);
  875. goto retry_grab;
  876. }
  877. wait_on_page_writeback(page);
  878. if (ext4_should_dioread_nolock(inode))
  879. ret = __block_write_begin(page, pos, len, ext4_get_block_write);
  880. else
  881. ret = __block_write_begin(page, pos, len, ext4_get_block);
  882. if (!ret && ext4_should_journal_data(inode)) {
  883. ret = ext4_walk_page_buffers(handle, page_buffers(page),
  884. from, to, NULL,
  885. do_journal_get_write_access);
  886. }
  887. if (ret) {
  888. unlock_page(page);
  889. /*
  890. * __block_write_begin may have instantiated a few blocks
  891. * outside i_size. Trim these off again. Don't need
  892. * i_size_read because we hold i_mutex.
  893. *
  894. * Add inode to orphan list in case we crash before
  895. * truncate finishes
  896. */
  897. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  898. ext4_orphan_add(handle, inode);
  899. ext4_journal_stop(handle);
  900. if (pos + len > inode->i_size) {
  901. ext4_truncate_failed_write(inode);
  902. /*
  903. * If truncate failed early the inode might
  904. * still be on the orphan list; we need to
  905. * make sure the inode is removed from the
  906. * orphan list in that case.
  907. */
  908. if (inode->i_nlink)
  909. ext4_orphan_del(NULL, inode);
  910. }
  911. if (ret == -ENOSPC &&
  912. ext4_should_retry_alloc(inode->i_sb, &retries))
  913. goto retry_journal;
  914. page_cache_release(page);
  915. return ret;
  916. }
  917. *pagep = page;
  918. return ret;
  919. }
  920. /* For write_end() in data=journal mode */
  921. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  922. {
  923. int ret;
  924. if (!buffer_mapped(bh) || buffer_freed(bh))
  925. return 0;
  926. set_buffer_uptodate(bh);
  927. ret = ext4_handle_dirty_metadata(handle, NULL, bh);
  928. clear_buffer_meta(bh);
  929. clear_buffer_prio(bh);
  930. return ret;
  931. }
  932. /*
  933. * We need to pick up the new inode size which generic_commit_write gave us
  934. * `file' can be NULL - eg, when called from page_symlink().
  935. *
  936. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  937. * buffers are managed internally.
  938. */
  939. static int ext4_write_end(struct file *file,
  940. struct address_space *mapping,
  941. loff_t pos, unsigned len, unsigned copied,
  942. struct page *page, void *fsdata)
  943. {
  944. handle_t *handle = ext4_journal_current_handle();
  945. struct inode *inode = mapping->host;
  946. int ret = 0, ret2;
  947. int i_size_changed = 0;
  948. trace_ext4_write_end(inode, pos, len, copied);
  949. if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) {
  950. ret = ext4_jbd2_file_inode(handle, inode);
  951. if (ret) {
  952. unlock_page(page);
  953. page_cache_release(page);
  954. goto errout;
  955. }
  956. }
  957. if (ext4_has_inline_data(inode))
  958. copied = ext4_write_inline_data_end(inode, pos, len,
  959. copied, page);
  960. else
  961. copied = block_write_end(file, mapping, pos,
  962. len, copied, page, fsdata);
  963. /*
  964. * No need to use i_size_read() here, the i_size
  965. * cannot change under us because we hole i_mutex.
  966. *
  967. * But it's important to update i_size while still holding page lock:
  968. * page writeout could otherwise come in and zero beyond i_size.
  969. */
  970. if (pos + copied > inode->i_size) {
  971. i_size_write(inode, pos + copied);
  972. i_size_changed = 1;
  973. }
  974. if (pos + copied > EXT4_I(inode)->i_disksize) {
  975. /* We need to mark inode dirty even if
  976. * new_i_size is less that inode->i_size
  977. * but greater than i_disksize. (hint delalloc)
  978. */
  979. ext4_update_i_disksize(inode, (pos + copied));
  980. i_size_changed = 1;
  981. }
  982. unlock_page(page);
  983. page_cache_release(page);
  984. /*
  985. * Don't mark the inode dirty under page lock. First, it unnecessarily
  986. * makes the holding time of page lock longer. Second, it forces lock
  987. * ordering of page lock and transaction start for journaling
  988. * filesystems.
  989. */
  990. if (i_size_changed)
  991. ext4_mark_inode_dirty(handle, inode);
  992. if (copied < 0)
  993. ret = copied;
  994. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  995. /* if we have allocated more blocks and copied
  996. * less. We will have blocks allocated outside
  997. * inode->i_size. So truncate them
  998. */
  999. ext4_orphan_add(handle, inode);
  1000. errout:
  1001. ret2 = ext4_journal_stop(handle);
  1002. if (!ret)
  1003. ret = ret2;
  1004. if (pos + len > inode->i_size) {
  1005. ext4_truncate_failed_write(inode);
  1006. /*
  1007. * If truncate failed early the inode might still be
  1008. * on the orphan list; we need to make sure the inode
  1009. * is removed from the orphan list in that case.
  1010. */
  1011. if (inode->i_nlink)
  1012. ext4_orphan_del(NULL, inode);
  1013. }
  1014. return ret ? ret : copied;
  1015. }
  1016. static int ext4_journalled_write_end(struct file *file,
  1017. struct address_space *mapping,
  1018. loff_t pos, unsigned len, unsigned copied,
  1019. struct page *page, void *fsdata)
  1020. {
  1021. handle_t *handle = ext4_journal_current_handle();
  1022. struct inode *inode = mapping->host;
  1023. int ret = 0, ret2;
  1024. int partial = 0;
  1025. unsigned from, to;
  1026. loff_t new_i_size;
  1027. trace_ext4_journalled_write_end(inode, pos, len, copied);
  1028. from = pos & (PAGE_CACHE_SIZE - 1);
  1029. to = from + len;
  1030. BUG_ON(!ext4_handle_valid(handle));
  1031. if (ext4_has_inline_data(inode))
  1032. copied = ext4_write_inline_data_end(inode, pos, len,
  1033. copied, page);
  1034. else {
  1035. if (copied < len) {
  1036. if (!PageUptodate(page))
  1037. copied = 0;
  1038. page_zero_new_buffers(page, from+copied, to);
  1039. }
  1040. ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
  1041. to, &partial, write_end_fn);
  1042. if (!partial)
  1043. SetPageUptodate(page);
  1044. }
  1045. new_i_size = pos + copied;
  1046. if (new_i_size > inode->i_size)
  1047. i_size_write(inode, pos+copied);
  1048. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1049. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1050. if (new_i_size > EXT4_I(inode)->i_disksize) {
  1051. ext4_update_i_disksize(inode, new_i_size);
  1052. ret2 = ext4_mark_inode_dirty(handle, inode);
  1053. if (!ret)
  1054. ret = ret2;
  1055. }
  1056. unlock_page(page);
  1057. page_cache_release(page);
  1058. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1059. /* if we have allocated more blocks and copied
  1060. * less. We will have blocks allocated outside
  1061. * inode->i_size. So truncate them
  1062. */
  1063. ext4_orphan_add(handle, inode);
  1064. ret2 = ext4_journal_stop(handle);
  1065. if (!ret)
  1066. ret = ret2;
  1067. if (pos + len > inode->i_size) {
  1068. ext4_truncate_failed_write(inode);
  1069. /*
  1070. * If truncate failed early the inode might still be
  1071. * on the orphan list; we need to make sure the inode
  1072. * is removed from the orphan list in that case.
  1073. */
  1074. if (inode->i_nlink)
  1075. ext4_orphan_del(NULL, inode);
  1076. }
  1077. return ret ? ret : copied;
  1078. }
  1079. /*
  1080. * Reserve a metadata for a single block located at lblock
  1081. */
  1082. static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
  1083. {
  1084. int retries = 0;
  1085. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1086. struct ext4_inode_info *ei = EXT4_I(inode);
  1087. unsigned int md_needed;
  1088. ext4_lblk_t save_last_lblock;
  1089. int save_len;
  1090. /*
  1091. * recalculate the amount of metadata blocks to reserve
  1092. * in order to allocate nrblocks
  1093. * worse case is one extent per block
  1094. */
  1095. repeat:
  1096. spin_lock(&ei->i_block_reservation_lock);
  1097. /*
  1098. * ext4_calc_metadata_amount() has side effects, which we have
  1099. * to be prepared undo if we fail to claim space.
  1100. */
  1101. save_len = ei->i_da_metadata_calc_len;
  1102. save_last_lblock = ei->i_da_metadata_calc_last_lblock;
  1103. md_needed = EXT4_NUM_B2C(sbi,
  1104. ext4_calc_metadata_amount(inode, lblock));
  1105. trace_ext4_da_reserve_space(inode, md_needed);
  1106. /*
  1107. * We do still charge estimated metadata to the sb though;
  1108. * we cannot afford to run out of free blocks.
  1109. */
  1110. if (ext4_claim_free_clusters(sbi, md_needed, 0)) {
  1111. ei->i_da_metadata_calc_len = save_len;
  1112. ei->i_da_metadata_calc_last_lblock = save_last_lblock;
  1113. spin_unlock(&ei->i_block_reservation_lock);
  1114. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  1115. cond_resched();
  1116. goto repeat;
  1117. }
  1118. return -ENOSPC;
  1119. }
  1120. ei->i_reserved_meta_blocks += md_needed;
  1121. spin_unlock(&ei->i_block_reservation_lock);
  1122. return 0; /* success */
  1123. }
  1124. /*
  1125. * Reserve a single cluster located at lblock
  1126. */
  1127. static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
  1128. {
  1129. int retries = 0;
  1130. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1131. struct ext4_inode_info *ei = EXT4_I(inode);
  1132. unsigned int md_needed;
  1133. int ret;
  1134. ext4_lblk_t save_last_lblock;
  1135. int save_len;
  1136. /*
  1137. * We will charge metadata quota at writeout time; this saves
  1138. * us from metadata over-estimation, though we may go over by
  1139. * a small amount in the end. Here we just reserve for data.
  1140. */
  1141. ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
  1142. if (ret)
  1143. return ret;
  1144. /*
  1145. * recalculate the amount of metadata blocks to reserve
  1146. * in order to allocate nrblocks
  1147. * worse case is one extent per block
  1148. */
  1149. repeat:
  1150. spin_lock(&ei->i_block_reservation_lock);
  1151. /*
  1152. * ext4_calc_metadata_amount() has side effects, which we have
  1153. * to be prepared undo if we fail to claim space.
  1154. */
  1155. save_len = ei->i_da_metadata_calc_len;
  1156. save_last_lblock = ei->i_da_metadata_calc_last_lblock;
  1157. md_needed = EXT4_NUM_B2C(sbi,
  1158. ext4_calc_metadata_amount(inode, lblock));
  1159. trace_ext4_da_reserve_space(inode, md_needed);
  1160. /*
  1161. * We do still charge estimated metadata to the sb though;
  1162. * we cannot afford to run out of free blocks.
  1163. */
  1164. if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
  1165. ei->i_da_metadata_calc_len = save_len;
  1166. ei->i_da_metadata_calc_last_lblock = save_last_lblock;
  1167. spin_unlock(&ei->i_block_reservation_lock);
  1168. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  1169. cond_resched();
  1170. goto repeat;
  1171. }
  1172. dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
  1173. return -ENOSPC;
  1174. }
  1175. ei->i_reserved_data_blocks++;
  1176. ei->i_reserved_meta_blocks += md_needed;
  1177. spin_unlock(&ei->i_block_reservation_lock);
  1178. return 0; /* success */
  1179. }
  1180. static void ext4_da_release_space(struct inode *inode, int to_free)
  1181. {
  1182. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1183. struct ext4_inode_info *ei = EXT4_I(inode);
  1184. if (!to_free)
  1185. return; /* Nothing to release, exit */
  1186. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1187. trace_ext4_da_release_space(inode, to_free);
  1188. if (unlikely(to_free > ei->i_reserved_data_blocks)) {
  1189. /*
  1190. * if there aren't enough reserved blocks, then the
  1191. * counter is messed up somewhere. Since this
  1192. * function is called from invalidate page, it's
  1193. * harmless to return without any action.
  1194. */
  1195. ext4_warning(inode->i_sb, "ext4_da_release_space: "
  1196. "ino %lu, to_free %d with only %d reserved "
  1197. "data blocks", inode->i_ino, to_free,
  1198. ei->i_reserved_data_blocks);
  1199. WARN_ON(1);
  1200. to_free = ei->i_reserved_data_blocks;
  1201. }
  1202. ei->i_reserved_data_blocks -= to_free;
  1203. if (ei->i_reserved_data_blocks == 0) {
  1204. /*
  1205. * We can release all of the reserved metadata blocks
  1206. * only when we have written all of the delayed
  1207. * allocation blocks.
  1208. * Note that in case of bigalloc, i_reserved_meta_blocks,
  1209. * i_reserved_data_blocks, etc. refer to number of clusters.
  1210. */
  1211. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  1212. ei->i_reserved_meta_blocks);
  1213. ei->i_reserved_meta_blocks = 0;
  1214. ei->i_da_metadata_calc_len = 0;
  1215. }
  1216. /* update fs dirty data blocks counter */
  1217. percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
  1218. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1219. dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
  1220. }
  1221. static void ext4_da_page_release_reservation(struct page *page,
  1222. unsigned int offset,
  1223. unsigned int length)
  1224. {
  1225. int to_release = 0;
  1226. struct buffer_head *head, *bh;
  1227. unsigned int curr_off = 0;
  1228. struct inode *inode = page->mapping->host;
  1229. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1230. unsigned int stop = offset + length;
  1231. int num_clusters;
  1232. ext4_fsblk_t lblk;
  1233. BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
  1234. head = page_buffers(page);
  1235. bh = head;
  1236. do {
  1237. unsigned int next_off = curr_off + bh->b_size;
  1238. if (next_off > stop)
  1239. break;
  1240. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1241. to_release++;
  1242. clear_buffer_delay(bh);
  1243. }
  1244. curr_off = next_off;
  1245. } while ((bh = bh->b_this_page) != head);
  1246. if (to_release) {
  1247. lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1248. ext4_es_remove_extent(inode, lblk, to_release);
  1249. }
  1250. /* If we have released all the blocks belonging to a cluster, then we
  1251. * need to release the reserved space for that cluster. */
  1252. num_clusters = EXT4_NUM_B2C(sbi, to_release);
  1253. while (num_clusters > 0) {
  1254. lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
  1255. ((num_clusters - 1) << sbi->s_cluster_bits);
  1256. if (sbi->s_cluster_ratio == 1 ||
  1257. !ext4_find_delalloc_cluster(inode, lblk))
  1258. ext4_da_release_space(inode, 1);
  1259. num_clusters--;
  1260. }
  1261. }
  1262. /*
  1263. * Delayed allocation stuff
  1264. */
  1265. struct mpage_da_data {
  1266. struct inode *inode;
  1267. struct writeback_control *wbc;
  1268. pgoff_t first_page; /* The first page to write */
  1269. pgoff_t next_page; /* Current page to examine */
  1270. pgoff_t last_page; /* Last page to examine */
  1271. /*
  1272. * Extent to map - this can be after first_page because that can be
  1273. * fully mapped. We somewhat abuse m_flags to store whether the extent
  1274. * is delalloc or unwritten.
  1275. */
  1276. struct ext4_map_blocks map;
  1277. struct ext4_io_submit io_submit; /* IO submission data */
  1278. };
  1279. static void mpage_release_unused_pages(struct mpage_da_data *mpd,
  1280. bool invalidate)
  1281. {
  1282. int nr_pages, i;
  1283. pgoff_t index, end;
  1284. struct pagevec pvec;
  1285. struct inode *inode = mpd->inode;
  1286. struct address_space *mapping = inode->i_mapping;
  1287. /* This is necessary when next_page == 0. */
  1288. if (mpd->first_page >= mpd->next_page)
  1289. return;
  1290. index = mpd->first_page;
  1291. end = mpd->next_page - 1;
  1292. if (invalidate) {
  1293. ext4_lblk_t start, last;
  1294. start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1295. last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1296. ext4_es_remove_extent(inode, start, last - start + 1);
  1297. }
  1298. pagevec_init(&pvec, 0);
  1299. while (index <= end) {
  1300. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1301. if (nr_pages == 0)
  1302. break;
  1303. for (i = 0; i < nr_pages; i++) {
  1304. struct page *page = pvec.pages[i];
  1305. if (page->index > end)
  1306. break;
  1307. BUG_ON(!PageLocked(page));
  1308. BUG_ON(PageWriteback(page));
  1309. if (invalidate) {
  1310. block_invalidatepage(page, 0, PAGE_CACHE_SIZE);
  1311. ClearPageUptodate(page);
  1312. }
  1313. unlock_page(page);
  1314. }
  1315. index = pvec.pages[nr_pages - 1]->index + 1;
  1316. pagevec_release(&pvec);
  1317. }
  1318. }
  1319. static void ext4_print_free_blocks(struct inode *inode)
  1320. {
  1321. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1322. struct super_block *sb = inode->i_sb;
  1323. struct ext4_inode_info *ei = EXT4_I(inode);
  1324. ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
  1325. EXT4_C2B(EXT4_SB(inode->i_sb),
  1326. ext4_count_free_clusters(sb)));
  1327. ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
  1328. ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
  1329. (long long) EXT4_C2B(EXT4_SB(sb),
  1330. percpu_counter_sum(&sbi->s_freeclusters_counter)));
  1331. ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
  1332. (long long) EXT4_C2B(EXT4_SB(sb),
  1333. percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
  1334. ext4_msg(sb, KERN_CRIT, "Block reservation details");
  1335. ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
  1336. ei->i_reserved_data_blocks);
  1337. ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
  1338. ei->i_reserved_meta_blocks);
  1339. ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u",
  1340. ei->i_allocated_meta_blocks);
  1341. return;
  1342. }
  1343. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  1344. {
  1345. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  1346. }
  1347. /*
  1348. * This function is grabs code from the very beginning of
  1349. * ext4_map_blocks, but assumes that the caller is from delayed write
  1350. * time. This function looks up the requested blocks and sets the
  1351. * buffer delay bit under the protection of i_data_sem.
  1352. */
  1353. static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
  1354. struct ext4_map_blocks *map,
  1355. struct buffer_head *bh)
  1356. {
  1357. struct extent_status es;
  1358. int retval;
  1359. sector_t invalid_block = ~((sector_t) 0xffff);
  1360. #ifdef ES_AGGRESSIVE_TEST
  1361. struct ext4_map_blocks orig_map;
  1362. memcpy(&orig_map, map, sizeof(*map));
  1363. #endif
  1364. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  1365. invalid_block = ~0;
  1366. map->m_flags = 0;
  1367. ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
  1368. "logical block %lu\n", inode->i_ino, map->m_len,
  1369. (unsigned long) map->m_lblk);
  1370. /* Lookup extent status tree firstly */
  1371. if (ext4_es_lookup_extent(inode, iblock, &es)) {
  1372. if (ext4_es_is_hole(&es)) {
  1373. retval = 0;
  1374. down_read((&EXT4_I(inode)->i_data_sem));
  1375. goto add_delayed;
  1376. }
  1377. /*
  1378. * Delayed extent could be allocated by fallocate.
  1379. * So we need to check it.
  1380. */
  1381. if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
  1382. map_bh(bh, inode->i_sb, invalid_block);
  1383. set_buffer_new(bh);
  1384. set_buffer_delay(bh);
  1385. return 0;
  1386. }
  1387. map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
  1388. retval = es.es_len - (iblock - es.es_lblk);
  1389. if (retval > map->m_len)
  1390. retval = map->m_len;
  1391. map->m_len = retval;
  1392. if (ext4_es_is_written(&es))
  1393. map->m_flags |= EXT4_MAP_MAPPED;
  1394. else if (ext4_es_is_unwritten(&es))
  1395. map->m_flags |= EXT4_MAP_UNWRITTEN;
  1396. else
  1397. BUG_ON(1);
  1398. #ifdef ES_AGGRESSIVE_TEST
  1399. ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
  1400. #endif
  1401. return retval;
  1402. }
  1403. /*
  1404. * Try to see if we can get the block without requesting a new
  1405. * file system block.
  1406. */
  1407. down_read((&EXT4_I(inode)->i_data_sem));
  1408. if (ext4_has_inline_data(inode)) {
  1409. /*
  1410. * We will soon create blocks for this page, and let
  1411. * us pretend as if the blocks aren't allocated yet.
  1412. * In case of clusters, we have to handle the work
  1413. * of mapping from cluster so that the reserved space
  1414. * is calculated properly.
  1415. */
  1416. if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
  1417. ext4_find_delalloc_cluster(inode, map->m_lblk))
  1418. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  1419. retval = 0;
  1420. } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  1421. retval = ext4_ext_map_blocks(NULL, inode, map,
  1422. EXT4_GET_BLOCKS_NO_PUT_HOLE);
  1423. else
  1424. retval = ext4_ind_map_blocks(NULL, inode, map,
  1425. EXT4_GET_BLOCKS_NO_PUT_HOLE);
  1426. add_delayed:
  1427. if (retval == 0) {
  1428. int ret;
  1429. /*
  1430. * XXX: __block_prepare_write() unmaps passed block,
  1431. * is it OK?
  1432. */
  1433. /*
  1434. * If the block was allocated from previously allocated cluster,
  1435. * then we don't need to reserve it again. However we still need
  1436. * to reserve metadata for every block we're going to write.
  1437. */
  1438. if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
  1439. ret = ext4_da_reserve_space(inode, iblock);
  1440. if (ret) {
  1441. /* not enough space to reserve */
  1442. retval = ret;
  1443. goto out_unlock;
  1444. }
  1445. } else {
  1446. ret = ext4_da_reserve_metadata(inode, iblock);
  1447. if (ret) {
  1448. /* not enough space to reserve */
  1449. retval = ret;
  1450. goto out_unlock;
  1451. }
  1452. }
  1453. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  1454. ~0, EXTENT_STATUS_DELAYED);
  1455. if (ret) {
  1456. retval = ret;
  1457. goto out_unlock;
  1458. }
  1459. /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
  1460. * and it should not appear on the bh->b_state.
  1461. */
  1462. map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
  1463. map_bh(bh, inode->i_sb, invalid_block);
  1464. set_buffer_new(bh);
  1465. set_buffer_delay(bh);
  1466. } else if (retval > 0) {
  1467. int ret;
  1468. unsigned long long status;
  1469. #ifdef ES_AGGRESSIVE_TEST
  1470. if (retval != map->m_len) {
  1471. printk("ES len assertation failed for inode: %lu "
  1472. "retval %d != map->m_len %d "
  1473. "in %s (lookup)\n", inode->i_ino, retval,
  1474. map->m_len, __func__);
  1475. }
  1476. #endif
  1477. status = map->m_flags & EXT4_MAP_UNWRITTEN ?
  1478. EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
  1479. ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
  1480. map->m_pblk, status);
  1481. if (ret != 0)
  1482. retval = ret;
  1483. }
  1484. out_unlock:
  1485. up_read((&EXT4_I(inode)->i_data_sem));
  1486. return retval;
  1487. }
  1488. /*
  1489. * This is a special get_blocks_t callback which is used by
  1490. * ext4_da_write_begin(). It will either return mapped block or
  1491. * reserve space for a single block.
  1492. *
  1493. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  1494. * We also have b_blocknr = -1 and b_bdev initialized properly
  1495. *
  1496. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  1497. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  1498. * initialized properly.
  1499. */
  1500. int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1501. struct buffer_head *bh, int create)
  1502. {
  1503. struct ext4_map_blocks map;
  1504. int ret = 0;
  1505. BUG_ON(create == 0);
  1506. BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
  1507. map.m_lblk = iblock;
  1508. map.m_len = 1;
  1509. /*
  1510. * first, we need to know whether the block is allocated already
  1511. * preallocated blocks are unmapped but should treated
  1512. * the same as allocated blocks.
  1513. */
  1514. ret = ext4_da_map_blocks(inode, iblock, &map, bh);
  1515. if (ret <= 0)
  1516. return ret;
  1517. map_bh(bh, inode->i_sb, map.m_pblk);
  1518. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  1519. if (buffer_unwritten(bh)) {
  1520. /* A delayed write to unwritten bh should be marked
  1521. * new and mapped. Mapped ensures that we don't do
  1522. * get_block multiple times when we write to the same
  1523. * offset and new ensures that we do proper zero out
  1524. * for partial write.
  1525. */
  1526. set_buffer_new(bh);
  1527. set_buffer_mapped(bh);
  1528. }
  1529. return 0;
  1530. }
  1531. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1532. {
  1533. get_bh(bh);
  1534. return 0;
  1535. }
  1536. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1537. {
  1538. put_bh(bh);
  1539. return 0;
  1540. }
  1541. static int __ext4_journalled_writepage(struct page *page,
  1542. unsigned int len)
  1543. {
  1544. struct address_space *mapping = page->mapping;
  1545. struct inode *inode = mapping->host;
  1546. struct buffer_head *page_bufs = NULL;
  1547. handle_t *handle = NULL;
  1548. int ret = 0, err = 0;
  1549. int inline_data = ext4_has_inline_data(inode);
  1550. struct buffer_head *inode_bh = NULL;
  1551. ClearPageChecked(page);
  1552. if (inline_data) {
  1553. BUG_ON(page->index != 0);
  1554. BUG_ON(len > ext4_get_max_inline_size(inode));
  1555. inode_bh = ext4_journalled_write_inline_data(inode, len, page);
  1556. if (inode_bh == NULL)
  1557. goto out;
  1558. } else {
  1559. page_bufs = page_buffers(page);
  1560. if (!page_bufs) {
  1561. BUG();
  1562. goto out;
  1563. }
  1564. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1565. NULL, bget_one);
  1566. }
  1567. /* As soon as we unlock the page, it can go away, but we have
  1568. * references to buffers so we are safe */
  1569. unlock_page(page);
  1570. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  1571. ext4_writepage_trans_blocks(inode));
  1572. if (IS_ERR(handle)) {
  1573. ret = PTR_ERR(handle);
  1574. goto out;
  1575. }
  1576. BUG_ON(!ext4_handle_valid(handle));
  1577. if (inline_data) {
  1578. ret = ext4_journal_get_write_access(handle, inode_bh);
  1579. err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
  1580. } else {
  1581. ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1582. do_journal_get_write_access);
  1583. err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1584. write_end_fn);
  1585. }
  1586. if (ret == 0)
  1587. ret = err;
  1588. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1589. err = ext4_journal_stop(handle);
  1590. if (!ret)
  1591. ret = err;
  1592. if (!ext4_has_inline_data(inode))
  1593. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1594. NULL, bput_one);
  1595. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1596. out:
  1597. brelse(inode_bh);
  1598. return ret;
  1599. }
  1600. /*
  1601. * Note that we don't need to start a transaction unless we're journaling data
  1602. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  1603. * need to file the inode to the transaction's list in ordered mode because if
  1604. * we are writing back data added by write(), the inode is already there and if
  1605. * we are writing back data modified via mmap(), no one guarantees in which
  1606. * transaction the data will hit the disk. In case we are journaling data, we
  1607. * cannot start transaction directly because transaction start ranks above page
  1608. * lock so we have to do some magic.
  1609. *
  1610. * This function can get called via...
  1611. * - ext4_da_writepages after taking page lock (have journal handle)
  1612. * - journal_submit_inode_data_buffers (no journal handle)
  1613. * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
  1614. * - grab_page_cache when doing write_begin (have journal handle)
  1615. *
  1616. * We don't do any block allocation in this function. If we have page with
  1617. * multiple blocks we need to write those buffer_heads that are mapped. This
  1618. * is important for mmaped based write. So if we do with blocksize 1K
  1619. * truncate(f, 1024);
  1620. * a = mmap(f, 0, 4096);
  1621. * a[0] = 'a';
  1622. * truncate(f, 4096);
  1623. * we have in the page first buffer_head mapped via page_mkwrite call back
  1624. * but other buffer_heads would be unmapped but dirty (dirty done via the
  1625. * do_wp_page). So writepage should write the first block. If we modify
  1626. * the mmap area beyond 1024 we will again get a page_fault and the
  1627. * page_mkwrite callback will do the block allocation and mark the
  1628. * buffer_heads mapped.
  1629. *
  1630. * We redirty the page if we have any buffer_heads that is either delay or
  1631. * unwritten in the page.
  1632. *
  1633. * We can get recursively called as show below.
  1634. *
  1635. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  1636. * ext4_writepage()
  1637. *
  1638. * But since we don't do any block allocation we should not deadlock.
  1639. * Page also have the dirty flag cleared so we don't get recurive page_lock.
  1640. */
  1641. static int ext4_writepage(struct page *page,
  1642. struct writeback_control *wbc)
  1643. {
  1644. int ret = 0;
  1645. loff_t size;
  1646. unsigned int len;
  1647. struct buffer_head *page_bufs = NULL;
  1648. struct inode *inode = page->mapping->host;
  1649. struct ext4_io_submit io_submit;
  1650. trace_ext4_writepage(page);
  1651. size = i_size_read(inode);
  1652. if (page->index == size >> PAGE_CACHE_SHIFT)
  1653. len = size & ~PAGE_CACHE_MASK;
  1654. else
  1655. len = PAGE_CACHE_SIZE;
  1656. page_bufs = page_buffers(page);
  1657. /*
  1658. * We cannot do block allocation or other extent handling in this
  1659. * function. If there are buffers needing that, we have to redirty
  1660. * the page. But we may reach here when we do a journal commit via
  1661. * journal_submit_inode_data_buffers() and in that case we must write
  1662. * allocated buffers to achieve data=ordered mode guarantees.
  1663. */
  1664. if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  1665. ext4_bh_delay_or_unwritten)) {
  1666. redirty_page_for_writepage(wbc, page);
  1667. if (current->flags & PF_MEMALLOC) {
  1668. /*
  1669. * For memory cleaning there's no point in writing only
  1670. * some buffers. So just bail out. Warn if we came here
  1671. * from direct reclaim.
  1672. */
  1673. WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
  1674. == PF_MEMALLOC);
  1675. unlock_page(page);
  1676. return 0;
  1677. }
  1678. }
  1679. if (PageChecked(page) && ext4_should_journal_data(inode))
  1680. /*
  1681. * It's mmapped pagecache. Add buffers and journal it. There
  1682. * doesn't seem much point in redirtying the page here.
  1683. */
  1684. return __ext4_journalled_writepage(page, len);
  1685. ext4_io_submit_init(&io_submit, wbc);
  1686. io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
  1687. if (!io_submit.io_end) {
  1688. redirty_page_for_writepage(wbc, page);
  1689. unlock_page(page);
  1690. return -ENOMEM;
  1691. }
  1692. ret = ext4_bio_write_page(&io_submit, page, len, wbc);
  1693. ext4_io_submit(&io_submit);
  1694. /* Drop io_end reference we got from init */
  1695. ext4_put_io_end_defer(io_submit.io_end);
  1696. return ret;
  1697. }
  1698. #define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
  1699. /*
  1700. * mballoc gives us at most this number of blocks...
  1701. * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
  1702. * The rest of mballoc seems to handle chunks upto full group size.
  1703. */
  1704. #define MAX_WRITEPAGES_EXTENT_LEN 2048
  1705. /*
  1706. * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
  1707. *
  1708. * @mpd - extent of blocks
  1709. * @lblk - logical number of the block in the file
  1710. * @b_state - b_state of the buffer head added
  1711. *
  1712. * the function is used to collect contig. blocks in same state
  1713. */
  1714. static int mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
  1715. unsigned long b_state)
  1716. {
  1717. struct ext4_map_blocks *map = &mpd->map;
  1718. /* Don't go larger than mballoc is willing to allocate */
  1719. if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
  1720. return 0;
  1721. /* First block in the extent? */
  1722. if (map->m_len == 0) {
  1723. map->m_lblk = lblk;
  1724. map->m_len = 1;
  1725. map->m_flags = b_state & BH_FLAGS;
  1726. return 1;
  1727. }
  1728. /* Can we merge the block to our big extent? */
  1729. if (lblk == map->m_lblk + map->m_len &&
  1730. (b_state & BH_FLAGS) == map->m_flags) {
  1731. map->m_len++;
  1732. return 1;
  1733. }
  1734. return 0;
  1735. }
  1736. static bool add_page_bufs_to_extent(struct mpage_da_data *mpd,
  1737. struct buffer_head *head,
  1738. struct buffer_head *bh,
  1739. ext4_lblk_t lblk)
  1740. {
  1741. struct inode *inode = mpd->inode;
  1742. ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
  1743. >> inode->i_blkbits;
  1744. do {
  1745. BUG_ON(buffer_locked(bh));
  1746. if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
  1747. (!buffer_delay(bh) && !buffer_unwritten(bh)) ||
  1748. lblk >= blocks) {
  1749. /* Found extent to map? */
  1750. if (mpd->map.m_len)
  1751. return false;
  1752. if (lblk >= blocks)
  1753. return true;
  1754. continue;
  1755. }
  1756. if (!mpage_add_bh_to_extent(mpd, lblk, bh->b_state))
  1757. return false;
  1758. } while (lblk++, (bh = bh->b_this_page) != head);
  1759. return true;
  1760. }
  1761. static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
  1762. {
  1763. int len;
  1764. loff_t size = i_size_read(mpd->inode);
  1765. int err;
  1766. BUG_ON(page->index != mpd->first_page);
  1767. if (page->index == size >> PAGE_CACHE_SHIFT)
  1768. len = size & ~PAGE_CACHE_MASK;
  1769. else
  1770. len = PAGE_CACHE_SIZE;
  1771. clear_page_dirty_for_io(page);
  1772. err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc);
  1773. if (!err)
  1774. mpd->wbc->nr_to_write--;
  1775. mpd->first_page++;
  1776. return err;
  1777. }
  1778. /*
  1779. * mpage_map_buffers - update buffers corresponding to changed extent and
  1780. * submit fully mapped pages for IO
  1781. *
  1782. * @mpd - description of extent to map, on return next extent to map
  1783. *
  1784. * Scan buffers corresponding to changed extent (we expect corresponding pages
  1785. * to be already locked) and update buffer state according to new extent state.
  1786. * We map delalloc buffers to their physical location, clear unwritten bits,
  1787. * and mark buffers as uninit when we perform writes to uninitialized extents
  1788. * and do extent conversion after IO is finished. If the last page is not fully
  1789. * mapped, we update @map to the next extent in the last page that needs
  1790. * mapping. Otherwise we submit the page for IO.
  1791. */
  1792. static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
  1793. {
  1794. struct pagevec pvec;
  1795. int nr_pages, i;
  1796. struct inode *inode = mpd->inode;
  1797. struct buffer_head *head, *bh;
  1798. int bpp_bits = PAGE_CACHE_SHIFT - inode->i_blkbits;
  1799. ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
  1800. >> inode->i_blkbits;
  1801. pgoff_t start, end;
  1802. ext4_lblk_t lblk;
  1803. sector_t pblock;
  1804. int err;
  1805. start = mpd->map.m_lblk >> bpp_bits;
  1806. end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
  1807. lblk = start << bpp_bits;
  1808. pblock = mpd->map.m_pblk;
  1809. pagevec_init(&pvec, 0);
  1810. while (start <= end) {
  1811. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
  1812. PAGEVEC_SIZE);
  1813. if (nr_pages == 0)
  1814. break;
  1815. for (i = 0; i < nr_pages; i++) {
  1816. struct page *page = pvec.pages[i];
  1817. if (page->index > end)
  1818. break;
  1819. /* Upto 'end' pages must be contiguous */
  1820. BUG_ON(page->index != start);
  1821. bh = head = page_buffers(page);
  1822. do {
  1823. if (lblk < mpd->map.m_lblk)
  1824. continue;
  1825. if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
  1826. /*
  1827. * Buffer after end of mapped extent.
  1828. * Find next buffer in the page to map.
  1829. */
  1830. mpd->map.m_len = 0;
  1831. mpd->map.m_flags = 0;
  1832. add_page_bufs_to_extent(mpd, head, bh,
  1833. lblk);
  1834. pagevec_release(&pvec);
  1835. return 0;
  1836. }
  1837. if (buffer_delay(bh)) {
  1838. clear_buffer_delay(bh);
  1839. bh->b_blocknr = pblock++;
  1840. }
  1841. clear_buffer_unwritten(bh);
  1842. } while (++lblk < blocks &&
  1843. (bh = bh->b_this_page) != head);
  1844. /*
  1845. * FIXME: This is going to break if dioread_nolock
  1846. * supports blocksize < pagesize as we will try to
  1847. * convert potentially unmapped parts of inode.
  1848. */
  1849. mpd->io_submit.io_end->size += PAGE_CACHE_SIZE;
  1850. /* Page fully mapped - let IO run! */
  1851. err = mpage_submit_page(mpd, page);
  1852. if (err < 0) {
  1853. pagevec_release(&pvec);
  1854. return err;
  1855. }
  1856. start++;
  1857. }
  1858. pagevec_release(&pvec);
  1859. }
  1860. /* Extent fully mapped and matches with page boundary. We are done. */
  1861. mpd->map.m_len = 0;
  1862. mpd->map.m_flags = 0;
  1863. return 0;
  1864. }
  1865. static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
  1866. {
  1867. struct inode *inode = mpd->inode;
  1868. struct ext4_map_blocks *map = &mpd->map;
  1869. int get_blocks_flags;
  1870. int err;
  1871. trace_ext4_da_write_pages_extent(inode, map);
  1872. /*
  1873. * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
  1874. * to convert an uninitialized extent to be initialized (in the case
  1875. * where we have written into one or more preallocated blocks). It is
  1876. * possible that we're going to need more metadata blocks than
  1877. * previously reserved. However we must not fail because we're in
  1878. * writeback and there is nothing we can do about it so it might result
  1879. * in data loss. So use reserved blocks to allocate metadata if
  1880. * possible.
  1881. *
  1882. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if the blocks
  1883. * in question are delalloc blocks. This affects functions in many
  1884. * different parts of the allocation call path. This flag exists
  1885. * primarily because we don't want to change *many* call functions, so
  1886. * ext4_map_blocks() will set the EXT4_STATE_DELALLOC_RESERVED flag
  1887. * once the inode's allocation semaphore is taken.
  1888. */
  1889. get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
  1890. EXT4_GET_BLOCKS_METADATA_NOFAIL;
  1891. if (ext4_should_dioread_nolock(inode))
  1892. get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
  1893. if (map->m_flags & (1 << BH_Delay))
  1894. get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
  1895. err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
  1896. if (err < 0)
  1897. return err;
  1898. if (map->m_flags & EXT4_MAP_UNINIT) {
  1899. if (!mpd->io_submit.io_end->handle &&
  1900. ext4_handle_valid(handle)) {
  1901. mpd->io_submit.io_end->handle = handle->h_rsv_handle;
  1902. handle->h_rsv_handle = NULL;
  1903. }
  1904. ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
  1905. }
  1906. BUG_ON(map->m_len == 0);
  1907. if (map->m_flags & EXT4_MAP_NEW) {
  1908. struct block_device *bdev = inode->i_sb->s_bdev;
  1909. int i;
  1910. for (i = 0; i < map->m_len; i++)
  1911. unmap_underlying_metadata(bdev, map->m_pblk + i);
  1912. }
  1913. return 0;
  1914. }
  1915. /*
  1916. * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
  1917. * mpd->len and submit pages underlying it for IO
  1918. *
  1919. * @handle - handle for journal operations
  1920. * @mpd - extent to map
  1921. *
  1922. * The function maps extent starting at mpd->lblk of length mpd->len. If it is
  1923. * delayed, blocks are allocated, if it is unwritten, we may need to convert
  1924. * them to initialized or split the described range from larger unwritten
  1925. * extent. Note that we need not map all the described range since allocation
  1926. * can return less blocks or the range is covered by more unwritten extents. We
  1927. * cannot map more because we are limited by reserved transaction credits. On
  1928. * the other hand we always make sure that the last touched page is fully
  1929. * mapped so that it can be written out (and thus forward progress is
  1930. * guaranteed). After mapping we submit all mapped pages for IO.
  1931. */
  1932. static int mpage_map_and_submit_extent(handle_t *handle,
  1933. struct mpage_da_data *mpd)
  1934. {
  1935. struct inode *inode = mpd->inode;
  1936. struct ext4_map_blocks *map = &mpd->map;
  1937. int err;
  1938. loff_t disksize;
  1939. mpd->io_submit.io_end->offset =
  1940. ((loff_t)map->m_lblk) << inode->i_blkbits;
  1941. while (map->m_len) {
  1942. err = mpage_map_one_extent(handle, mpd);
  1943. if (err < 0) {
  1944. struct super_block *sb = inode->i_sb;
  1945. /*
  1946. * Need to commit transaction to free blocks. Let upper
  1947. * layers sort it out.
  1948. */
  1949. if (err == -ENOSPC && ext4_count_free_clusters(sb))
  1950. return -ENOSPC;
  1951. if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
  1952. ext4_msg(sb, KERN_CRIT,
  1953. "Delayed block allocation failed for "
  1954. "inode %lu at logical offset %llu with"
  1955. " max blocks %u with error %d",
  1956. inode->i_ino,
  1957. (unsigned long long)map->m_lblk,
  1958. (unsigned)map->m_len, err);
  1959. ext4_msg(sb, KERN_CRIT,
  1960. "This should not happen!! Data will "
  1961. "be lost\n");
  1962. if (err == -ENOSPC)
  1963. ext4_print_free_blocks(inode);
  1964. }
  1965. /* invalidate all the pages */
  1966. mpage_release_unused_pages(mpd, true);
  1967. return err;
  1968. }
  1969. /*
  1970. * Update buffer state, submit mapped pages, and get us new
  1971. * extent to map
  1972. */
  1973. err = mpage_map_and_submit_buffers(mpd);
  1974. if (err < 0)
  1975. return err;
  1976. }
  1977. /* Update on-disk size after IO is submitted */
  1978. disksize = ((loff_t)mpd->first_page) << PAGE_CACHE_SHIFT;
  1979. if (disksize > i_size_read(inode))
  1980. disksize = i_size_read(inode);
  1981. if (disksize > EXT4_I(inode)->i_disksize) {
  1982. int err2;
  1983. ext4_update_i_disksize(inode, disksize);
  1984. err2 = ext4_mark_inode_dirty(handle, inode);
  1985. if (err2)
  1986. ext4_error(inode->i_sb,
  1987. "Failed to mark inode %lu dirty",
  1988. inode->i_ino);
  1989. if (!err)
  1990. err = err2;
  1991. }
  1992. return err;
  1993. }
  1994. /*
  1995. * Calculate the total number of credits to reserve for one writepages
  1996. * iteration. This is called from ext4_da_writepages(). We map an extent of
  1997. * upto MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
  1998. * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
  1999. * bpp - 1 blocks in bpp different extents.
  2000. */
  2001. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  2002. {
  2003. int bpp = ext4_journal_blocks_per_page(inode);
  2004. return ext4_meta_trans_blocks(inode,
  2005. MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
  2006. }
  2007. /*
  2008. * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
  2009. * and underlying extent to map
  2010. *
  2011. * @mpd - where to look for pages
  2012. *
  2013. * Walk dirty pages in the mapping. If they are fully mapped, submit them for
  2014. * IO immediately. When we find a page which isn't mapped we start accumulating
  2015. * extent of buffers underlying these pages that needs mapping (formed by
  2016. * either delayed or unwritten buffers). We also lock the pages containing
  2017. * these buffers. The extent found is returned in @mpd structure (starting at
  2018. * mpd->lblk with length mpd->len blocks).
  2019. *
  2020. * Note that this function can attach bios to one io_end structure which are
  2021. * neither logically nor physically contiguous. Although it may seem as an
  2022. * unnecessary complication, it is actually inevitable in blocksize < pagesize
  2023. * case as we need to track IO to all buffers underlying a page in one io_end.
  2024. */
  2025. static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
  2026. {
  2027. struct address_space *mapping = mpd->inode->i_mapping;
  2028. struct pagevec pvec;
  2029. unsigned int nr_pages;
  2030. pgoff_t index = mpd->first_page;
  2031. pgoff_t end = mpd->last_page;
  2032. int tag;
  2033. int i, err = 0;
  2034. int blkbits = mpd->inode->i_blkbits;
  2035. ext4_lblk_t lblk;
  2036. struct buffer_head *head;
  2037. if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
  2038. tag = PAGECACHE_TAG_TOWRITE;
  2039. else
  2040. tag = PAGECACHE_TAG_DIRTY;
  2041. pagevec_init(&pvec, 0);
  2042. mpd->map.m_len = 0;
  2043. mpd->next_page = index;
  2044. while (index <= end) {
  2045. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  2046. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  2047. if (nr_pages == 0)
  2048. goto out;
  2049. for (i = 0; i < nr_pages; i++) {
  2050. struct page *page = pvec.pages[i];
  2051. /*
  2052. * At this point, the page may be truncated or
  2053. * invalidated (changing page->mapping to NULL), or
  2054. * even swizzled back from swapper_space to tmpfs file
  2055. * mapping. However, page->index will not change
  2056. * because we have a reference on the page.
  2057. */
  2058. if (page->index > end)
  2059. goto out;
  2060. /* If we can't merge this page, we are done. */
  2061. if (mpd->map.m_len > 0 && mpd->next_page != page->index)
  2062. goto out;
  2063. lock_page(page);
  2064. /*
  2065. * If the page is no longer dirty, or its mapping no
  2066. * longer corresponds to inode we are writing (which
  2067. * means it has been truncated or invalidated), or the
  2068. * page is already under writeback and we are not doing
  2069. * a data integrity writeback, skip the page
  2070. */
  2071. if (!PageDirty(page) ||
  2072. (PageWriteback(page) &&
  2073. (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
  2074. unlikely(page->mapping != mapping)) {
  2075. unlock_page(page);
  2076. continue;
  2077. }
  2078. wait_on_page_writeback(page);
  2079. BUG_ON(PageWriteback(page));
  2080. if (mpd->map.m_len == 0)
  2081. mpd->first_page = page->index;
  2082. mpd->next_page = page->index + 1;
  2083. /* Add all dirty buffers to mpd */
  2084. lblk = ((ext4_lblk_t)page->index) <<
  2085. (PAGE_CACHE_SHIFT - blkbits);
  2086. head = page_buffers(page);
  2087. if (!add_page_bufs_to_extent(mpd, head, head, lblk))
  2088. goto out;
  2089. /* So far everything mapped? Submit the page for IO. */
  2090. if (mpd->map.m_len == 0) {
  2091. err = mpage_submit_page(mpd, page);
  2092. if (err < 0)
  2093. goto out;
  2094. }
  2095. /*
  2096. * Accumulated enough dirty pages? This doesn't apply
  2097. * to WB_SYNC_ALL mode. For integrity sync we have to
  2098. * keep going because someone may be concurrently
  2099. * dirtying pages, and we might have synced a lot of
  2100. * newly appeared dirty pages, but have not synced all
  2101. * of the old dirty pages.
  2102. */
  2103. if (mpd->wbc->sync_mode == WB_SYNC_NONE &&
  2104. mpd->next_page - mpd->first_page >=
  2105. mpd->wbc->nr_to_write)
  2106. goto out;
  2107. }
  2108. pagevec_release(&pvec);
  2109. cond_resched();
  2110. }
  2111. return 0;
  2112. out:
  2113. pagevec_release(&pvec);
  2114. return err;
  2115. }
  2116. static int ext4_da_writepages(struct address_space *mapping,
  2117. struct writeback_control *wbc)
  2118. {
  2119. pgoff_t writeback_index = 0;
  2120. long nr_to_write = wbc->nr_to_write;
  2121. int range_whole = 0;
  2122. int cycled = 1;
  2123. handle_t *handle = NULL;
  2124. struct mpage_da_data mpd;
  2125. struct inode *inode = mapping->host;
  2126. int needed_blocks, rsv_blocks = 0, ret = 0;
  2127. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2128. bool done;
  2129. struct blk_plug plug;
  2130. trace_ext4_da_writepages(inode, wbc);
  2131. /*
  2132. * No pages to write? This is mainly a kludge to avoid starting
  2133. * a transaction for special inodes like journal inode on last iput()
  2134. * because that could violate lock ordering on umount
  2135. */
  2136. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2137. return 0;
  2138. /*
  2139. * If the filesystem has aborted, it is read-only, so return
  2140. * right away instead of dumping stack traces later on that
  2141. * will obscure the real source of the problem. We test
  2142. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  2143. * the latter could be true if the filesystem is mounted
  2144. * read-only, and in that case, ext4_da_writepages should
  2145. * *never* be called, so if that ever happens, we would want
  2146. * the stack trace.
  2147. */
  2148. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
  2149. return -EROFS;
  2150. if (ext4_should_dioread_nolock(inode)) {
  2151. /*
  2152. * We may need to convert upto one extent per block in
  2153. * the page and we may dirty the inode.
  2154. */
  2155. rsv_blocks = 1 + (PAGE_CACHE_SIZE >> inode->i_blkbits);
  2156. }
  2157. /*
  2158. * If we have inline data and arrive here, it means that
  2159. * we will soon create the block for the 1st page, so
  2160. * we'd better clear the inline data here.
  2161. */
  2162. if (ext4_has_inline_data(inode)) {
  2163. /* Just inode will be modified... */
  2164. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  2165. if (IS_ERR(handle)) {
  2166. ret = PTR_ERR(handle);
  2167. goto out_writepages;
  2168. }
  2169. BUG_ON(ext4_test_inode_state(inode,
  2170. EXT4_STATE_MAY_INLINE_DATA));
  2171. ext4_destroy_inline_data(handle, inode);
  2172. ext4_journal_stop(handle);
  2173. }
  2174. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2175. range_whole = 1;
  2176. if (wbc->range_cyclic) {
  2177. writeback_index = mapping->writeback_index;
  2178. if (writeback_index)
  2179. cycled = 0;
  2180. mpd.first_page = writeback_index;
  2181. mpd.last_page = -1;
  2182. } else {
  2183. mpd.first_page = wbc->range_start >> PAGE_CACHE_SHIFT;
  2184. mpd.last_page = wbc->range_end >> PAGE_CACHE_SHIFT;
  2185. }
  2186. mpd.inode = inode;
  2187. mpd.wbc = wbc;
  2188. ext4_io_submit_init(&mpd.io_submit, wbc);
  2189. retry:
  2190. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2191. tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
  2192. done = false;
  2193. blk_start_plug(&plug);
  2194. while (!done && mpd.first_page <= mpd.last_page) {
  2195. /* For each extent of pages we use new io_end */
  2196. mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
  2197. if (!mpd.io_submit.io_end) {
  2198. ret = -ENOMEM;
  2199. break;
  2200. }
  2201. /*
  2202. * We have two constraints: We find one extent to map and we
  2203. * must always write out whole page (makes a difference when
  2204. * blocksize < pagesize) so that we don't block on IO when we
  2205. * try to write out the rest of the page. Journalled mode is
  2206. * not supported by delalloc.
  2207. */
  2208. BUG_ON(ext4_should_journal_data(inode));
  2209. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2210. /* start a new transaction */
  2211. handle = ext4_journal_start_with_reserve(inode,
  2212. EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
  2213. if (IS_ERR(handle)) {
  2214. ret = PTR_ERR(handle);
  2215. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2216. "%ld pages, ino %lu; err %d", __func__,
  2217. wbc->nr_to_write, inode->i_ino, ret);
  2218. /* Release allocated io_end */
  2219. ext4_put_io_end(mpd.io_submit.io_end);
  2220. break;
  2221. }
  2222. trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
  2223. ret = mpage_prepare_extent_to_map(&mpd);
  2224. if (!ret) {
  2225. if (mpd.map.m_len)
  2226. ret = mpage_map_and_submit_extent(handle, &mpd);
  2227. else {
  2228. /*
  2229. * We scanned the whole range (or exhausted
  2230. * nr_to_write), submitted what was mapped and
  2231. * didn't find anything needing mapping. We are
  2232. * done.
  2233. */
  2234. done = true;
  2235. }
  2236. }
  2237. ext4_journal_stop(handle);
  2238. /* Submit prepared bio */
  2239. ext4_io_submit(&mpd.io_submit);
  2240. /* Unlock pages we didn't use */
  2241. mpage_release_unused_pages(&mpd, false);
  2242. /* Drop our io_end reference we got from init */
  2243. ext4_put_io_end(mpd.io_submit.io_end);
  2244. if (ret == -ENOSPC && sbi->s_journal) {
  2245. /*
  2246. * Commit the transaction which would
  2247. * free blocks released in the transaction
  2248. * and try again
  2249. */
  2250. jbd2_journal_force_commit_nested(sbi->s_journal);
  2251. ret = 0;
  2252. continue;
  2253. }
  2254. /* Fatal error - ENOMEM, EIO... */
  2255. if (ret)
  2256. break;
  2257. }
  2258. blk_finish_plug(&plug);
  2259. if (!ret && !cycled) {
  2260. cycled = 1;
  2261. mpd.last_page = writeback_index - 1;
  2262. mpd.first_page = 0;
  2263. goto retry;
  2264. }
  2265. /* Update index */
  2266. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2267. /*
  2268. * Set the writeback_index so that range_cyclic
  2269. * mode will write it back later
  2270. */
  2271. mapping->writeback_index = mpd.first_page;
  2272. out_writepages:
  2273. trace_ext4_da_writepages_result(inode, wbc, ret,
  2274. nr_to_write - wbc->nr_to_write);
  2275. return ret;
  2276. }
  2277. static int ext4_nonda_switch(struct super_block *sb)
  2278. {
  2279. s64 free_clusters, dirty_clusters;
  2280. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2281. /*
  2282. * switch to non delalloc mode if we are running low
  2283. * on free block. The free block accounting via percpu
  2284. * counters can get slightly wrong with percpu_counter_batch getting
  2285. * accumulated on each CPU without updating global counters
  2286. * Delalloc need an accurate free block accounting. So switch
  2287. * to non delalloc when we are near to error range.
  2288. */
  2289. free_clusters =
  2290. percpu_counter_read_positive(&sbi->s_freeclusters_counter);
  2291. dirty_clusters =
  2292. percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
  2293. /*
  2294. * Start pushing delalloc when 1/2 of free blocks are dirty.
  2295. */
  2296. if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
  2297. try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
  2298. if (2 * free_clusters < 3 * dirty_clusters ||
  2299. free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
  2300. /*
  2301. * free block count is less than 150% of dirty blocks
  2302. * or free blocks is less than watermark
  2303. */
  2304. return 1;
  2305. }
  2306. return 0;
  2307. }
  2308. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2309. loff_t pos, unsigned len, unsigned flags,
  2310. struct page **pagep, void **fsdata)
  2311. {
  2312. int ret, retries = 0;
  2313. struct page *page;
  2314. pgoff_t index;
  2315. struct inode *inode = mapping->host;
  2316. handle_t *handle;
  2317. index = pos >> PAGE_CACHE_SHIFT;
  2318. if (ext4_nonda_switch(inode->i_sb)) {
  2319. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2320. return ext4_write_begin(file, mapping, pos,
  2321. len, flags, pagep, fsdata);
  2322. }
  2323. *fsdata = (void *)0;
  2324. trace_ext4_da_write_begin(inode, pos, len, flags);
  2325. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2326. ret = ext4_da_write_inline_data_begin(mapping, inode,
  2327. pos, len, flags,
  2328. pagep, fsdata);
  2329. if (ret < 0)
  2330. return ret;
  2331. if (ret == 1)
  2332. return 0;
  2333. }
  2334. /*
  2335. * grab_cache_page_write_begin() can take a long time if the
  2336. * system is thrashing due to memory pressure, or if the page
  2337. * is being written back. So grab it first before we start
  2338. * the transaction handle. This also allows us to allocate
  2339. * the page (if needed) without using GFP_NOFS.
  2340. */
  2341. retry_grab:
  2342. page = grab_cache_page_write_begin(mapping, index, flags);
  2343. if (!page)
  2344. return -ENOMEM;
  2345. unlock_page(page);
  2346. /*
  2347. * With delayed allocation, we don't log the i_disksize update
  2348. * if there is delayed block allocation. But we still need
  2349. * to journalling the i_disksize update if writes to the end
  2350. * of file which has an already mapped buffer.
  2351. */
  2352. retry_journal:
  2353. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1);
  2354. if (IS_ERR(handle)) {
  2355. page_cache_release(page);
  2356. return PTR_ERR(handle);
  2357. }
  2358. lock_page(page);
  2359. if (page->mapping != mapping) {
  2360. /* The page got truncated from under us */
  2361. unlock_page(page);
  2362. page_cache_release(page);
  2363. ext4_journal_stop(handle);
  2364. goto retry_grab;
  2365. }
  2366. /* In case writeback began while the page was unlocked */
  2367. wait_on_page_writeback(page);
  2368. ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
  2369. if (ret < 0) {
  2370. unlock_page(page);
  2371. ext4_journal_stop(handle);
  2372. /*
  2373. * block_write_begin may have instantiated a few blocks
  2374. * outside i_size. Trim these off again. Don't need
  2375. * i_size_read because we hold i_mutex.
  2376. */
  2377. if (pos + len > inode->i_size)
  2378. ext4_truncate_failed_write(inode);
  2379. if (ret == -ENOSPC &&
  2380. ext4_should_retry_alloc(inode->i_sb, &retries))
  2381. goto retry_journal;
  2382. page_cache_release(page);
  2383. return ret;
  2384. }
  2385. *pagep = page;
  2386. return ret;
  2387. }
  2388. /*
  2389. * Check if we should update i_disksize
  2390. * when write to the end of file but not require block allocation
  2391. */
  2392. static int ext4_da_should_update_i_disksize(struct page *page,
  2393. unsigned long offset)
  2394. {
  2395. struct buffer_head *bh;
  2396. struct inode *inode = page->mapping->host;
  2397. unsigned int idx;
  2398. int i;
  2399. bh = page_buffers(page);
  2400. idx = offset >> inode->i_blkbits;
  2401. for (i = 0; i < idx; i++)
  2402. bh = bh->b_this_page;
  2403. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2404. return 0;
  2405. return 1;
  2406. }
  2407. static int ext4_da_write_end(struct file *file,
  2408. struct address_space *mapping,
  2409. loff_t pos, unsigned len, unsigned copied,
  2410. struct page *page, void *fsdata)
  2411. {
  2412. struct inode *inode = mapping->host;
  2413. int ret = 0, ret2;
  2414. handle_t *handle = ext4_journal_current_handle();
  2415. loff_t new_i_size;
  2416. unsigned long start, end;
  2417. int write_mode = (int)(unsigned long)fsdata;
  2418. if (write_mode == FALL_BACK_TO_NONDELALLOC)
  2419. return ext4_write_end(file, mapping, pos,
  2420. len, copied, page, fsdata);
  2421. trace_ext4_da_write_end(inode, pos, len, copied);
  2422. start = pos & (PAGE_CACHE_SIZE - 1);
  2423. end = start + copied - 1;
  2424. /*
  2425. * generic_write_end() will run mark_inode_dirty() if i_size
  2426. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2427. * into that.
  2428. */
  2429. new_i_size = pos + copied;
  2430. if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
  2431. if (ext4_has_inline_data(inode) ||
  2432. ext4_da_should_update_i_disksize(page, end)) {
  2433. down_write(&EXT4_I(inode)->i_data_sem);
  2434. if (new_i_size > EXT4_I(inode)->i_disksize)
  2435. EXT4_I(inode)->i_disksize = new_i_size;
  2436. up_write(&EXT4_I(inode)->i_data_sem);
  2437. /* We need to mark inode dirty even if
  2438. * new_i_size is less that inode->i_size
  2439. * bu greater than i_disksize.(hint delalloc)
  2440. */
  2441. ext4_mark_inode_dirty(handle, inode);
  2442. }
  2443. }
  2444. if (write_mode != CONVERT_INLINE_DATA &&
  2445. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
  2446. ext4_has_inline_data(inode))
  2447. ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
  2448. page);
  2449. else
  2450. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2451. page, fsdata);
  2452. copied = ret2;
  2453. if (ret2 < 0)
  2454. ret = ret2;
  2455. ret2 = ext4_journal_stop(handle);
  2456. if (!ret)
  2457. ret = ret2;
  2458. return ret ? ret : copied;
  2459. }
  2460. static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
  2461. unsigned int length)
  2462. {
  2463. /*
  2464. * Drop reserved blocks
  2465. */
  2466. BUG_ON(!PageLocked(page));
  2467. if (!page_has_buffers(page))
  2468. goto out;
  2469. ext4_da_page_release_reservation(page, offset, length);
  2470. out:
  2471. ext4_invalidatepage(page, offset, length);
  2472. return;
  2473. }
  2474. /*
  2475. * Force all delayed allocation blocks to be allocated for a given inode.
  2476. */
  2477. int ext4_alloc_da_blocks(struct inode *inode)
  2478. {
  2479. trace_ext4_alloc_da_blocks(inode);
  2480. if (!EXT4_I(inode)->i_reserved_data_blocks &&
  2481. !EXT4_I(inode)->i_reserved_meta_blocks)
  2482. return 0;
  2483. /*
  2484. * We do something simple for now. The filemap_flush() will
  2485. * also start triggering a write of the data blocks, which is
  2486. * not strictly speaking necessary (and for users of
  2487. * laptop_mode, not even desirable). However, to do otherwise
  2488. * would require replicating code paths in:
  2489. *
  2490. * ext4_da_writepages() ->
  2491. * write_cache_pages() ---> (via passed in callback function)
  2492. * __mpage_da_writepage() -->
  2493. * mpage_add_bh_to_extent()
  2494. * mpage_da_map_blocks()
  2495. *
  2496. * The problem is that write_cache_pages(), located in
  2497. * mm/page-writeback.c, marks pages clean in preparation for
  2498. * doing I/O, which is not desirable if we're not planning on
  2499. * doing I/O at all.
  2500. *
  2501. * We could call write_cache_pages(), and then redirty all of
  2502. * the pages by calling redirty_page_for_writepage() but that
  2503. * would be ugly in the extreme. So instead we would need to
  2504. * replicate parts of the code in the above functions,
  2505. * simplifying them because we wouldn't actually intend to
  2506. * write out the pages, but rather only collect contiguous
  2507. * logical block extents, call the multi-block allocator, and
  2508. * then update the buffer heads with the block allocations.
  2509. *
  2510. * For now, though, we'll cheat by calling filemap_flush(),
  2511. * which will map the blocks, and start the I/O, but not
  2512. * actually wait for the I/O to complete.
  2513. */
  2514. return filemap_flush(inode->i_mapping);
  2515. }
  2516. /*
  2517. * bmap() is special. It gets used by applications such as lilo and by
  2518. * the swapper to find the on-disk block of a specific piece of data.
  2519. *
  2520. * Naturally, this is dangerous if the block concerned is still in the
  2521. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2522. * filesystem and enables swap, then they may get a nasty shock when the
  2523. * data getting swapped to that swapfile suddenly gets overwritten by
  2524. * the original zero's written out previously to the journal and
  2525. * awaiting writeback in the kernel's buffer cache.
  2526. *
  2527. * So, if we see any bmap calls here on a modified, data-journaled file,
  2528. * take extra steps to flush any blocks which might be in the cache.
  2529. */
  2530. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2531. {
  2532. struct inode *inode = mapping->host;
  2533. journal_t *journal;
  2534. int err;
  2535. /*
  2536. * We can get here for an inline file via the FIBMAP ioctl
  2537. */
  2538. if (ext4_has_inline_data(inode))
  2539. return 0;
  2540. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2541. test_opt(inode->i_sb, DELALLOC)) {
  2542. /*
  2543. * With delalloc we want to sync the file
  2544. * so that we can make sure we allocate
  2545. * blocks for file
  2546. */
  2547. filemap_write_and_wait(mapping);
  2548. }
  2549. if (EXT4_JOURNAL(inode) &&
  2550. ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
  2551. /*
  2552. * This is a REALLY heavyweight approach, but the use of
  2553. * bmap on dirty files is expected to be extremely rare:
  2554. * only if we run lilo or swapon on a freshly made file
  2555. * do we expect this to happen.
  2556. *
  2557. * (bmap requires CAP_SYS_RAWIO so this does not
  2558. * represent an unprivileged user DOS attack --- we'd be
  2559. * in trouble if mortal users could trigger this path at
  2560. * will.)
  2561. *
  2562. * NB. EXT4_STATE_JDATA is not set on files other than
  2563. * regular files. If somebody wants to bmap a directory
  2564. * or symlink and gets confused because the buffer
  2565. * hasn't yet been flushed to disk, they deserve
  2566. * everything they get.
  2567. */
  2568. ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
  2569. journal = EXT4_JOURNAL(inode);
  2570. jbd2_journal_lock_updates(journal);
  2571. err = jbd2_journal_flush(journal);
  2572. jbd2_journal_unlock_updates(journal);
  2573. if (err)
  2574. return 0;
  2575. }
  2576. return generic_block_bmap(mapping, block, ext4_get_block);
  2577. }
  2578. static int ext4_readpage(struct file *file, struct page *page)
  2579. {
  2580. int ret = -EAGAIN;
  2581. struct inode *inode = page->mapping->host;
  2582. trace_ext4_readpage(page);
  2583. if (ext4_has_inline_data(inode))
  2584. ret = ext4_readpage_inline(inode, page);
  2585. if (ret == -EAGAIN)
  2586. return mpage_readpage(page, ext4_get_block);
  2587. return ret;
  2588. }
  2589. static int
  2590. ext4_readpages(struct file *file, struct address_space *mapping,
  2591. struct list_head *pages, unsigned nr_pages)
  2592. {
  2593. struct inode *inode = mapping->host;
  2594. /* If the file has inline data, no need to do readpages. */
  2595. if (ext4_has_inline_data(inode))
  2596. return 0;
  2597. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2598. }
  2599. static void ext4_invalidatepage(struct page *page, unsigned int offset,
  2600. unsigned int length)
  2601. {
  2602. trace_ext4_invalidatepage(page, offset, length);
  2603. /* No journalling happens on data buffers when this function is used */
  2604. WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
  2605. block_invalidatepage(page, offset, length);
  2606. }
  2607. static int __ext4_journalled_invalidatepage(struct page *page,
  2608. unsigned int offset,
  2609. unsigned int length)
  2610. {
  2611. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2612. trace_ext4_journalled_invalidatepage(page, offset, length);
  2613. /*
  2614. * If it's a full truncate we just forget about the pending dirtying
  2615. */
  2616. if (offset == 0 && length == PAGE_CACHE_SIZE)
  2617. ClearPageChecked(page);
  2618. return jbd2_journal_invalidatepage(journal, page, offset, length);
  2619. }
  2620. /* Wrapper for aops... */
  2621. static void ext4_journalled_invalidatepage(struct page *page,
  2622. unsigned int offset,
  2623. unsigned int length)
  2624. {
  2625. WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
  2626. }
  2627. static int ext4_releasepage(struct page *page, gfp_t wait)
  2628. {
  2629. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2630. trace_ext4_releasepage(page);
  2631. /* Page has dirty journalled data -> cannot release */
  2632. if (PageChecked(page))
  2633. return 0;
  2634. if (journal)
  2635. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2636. else
  2637. return try_to_free_buffers(page);
  2638. }
  2639. /*
  2640. * ext4_get_block used when preparing for a DIO write or buffer write.
  2641. * We allocate an uinitialized extent if blocks haven't been allocated.
  2642. * The extent will be converted to initialized after the IO is complete.
  2643. */
  2644. int ext4_get_block_write(struct inode *inode, sector_t iblock,
  2645. struct buffer_head *bh_result, int create)
  2646. {
  2647. ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
  2648. inode->i_ino, create);
  2649. return _ext4_get_block(inode, iblock, bh_result,
  2650. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  2651. }
  2652. static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
  2653. struct buffer_head *bh_result, int create)
  2654. {
  2655. ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
  2656. inode->i_ino, create);
  2657. return _ext4_get_block(inode, iblock, bh_result,
  2658. EXT4_GET_BLOCKS_NO_LOCK);
  2659. }
  2660. static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
  2661. ssize_t size, void *private, int ret,
  2662. bool is_async)
  2663. {
  2664. struct inode *inode = file_inode(iocb->ki_filp);
  2665. ext4_io_end_t *io_end = iocb->private;
  2666. /* if not async direct IO just return */
  2667. if (!io_end) {
  2668. inode_dio_done(inode);
  2669. if (is_async)
  2670. aio_complete(iocb, ret, 0);
  2671. return;
  2672. }
  2673. ext_debug("ext4_end_io_dio(): io_end 0x%p "
  2674. "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
  2675. iocb->private, io_end->inode->i_ino, iocb, offset,
  2676. size);
  2677. iocb->private = NULL;
  2678. io_end->offset = offset;
  2679. io_end->size = size;
  2680. if (is_async) {
  2681. io_end->iocb = iocb;
  2682. io_end->result = ret;
  2683. }
  2684. ext4_put_io_end_defer(io_end);
  2685. }
  2686. /*
  2687. * For ext4 extent files, ext4 will do direct-io write to holes,
  2688. * preallocated extents, and those write extend the file, no need to
  2689. * fall back to buffered IO.
  2690. *
  2691. * For holes, we fallocate those blocks, mark them as uninitialized
  2692. * If those blocks were preallocated, we mark sure they are split, but
  2693. * still keep the range to write as uninitialized.
  2694. *
  2695. * The unwritten extents will be converted to written when DIO is completed.
  2696. * For async direct IO, since the IO may still pending when return, we
  2697. * set up an end_io call back function, which will do the conversion
  2698. * when async direct IO completed.
  2699. *
  2700. * If the O_DIRECT write will extend the file then add this inode to the
  2701. * orphan list. So recovery will truncate it back to the original size
  2702. * if the machine crashes during the write.
  2703. *
  2704. */
  2705. static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
  2706. const struct iovec *iov, loff_t offset,
  2707. unsigned long nr_segs)
  2708. {
  2709. struct file *file = iocb->ki_filp;
  2710. struct inode *inode = file->f_mapping->host;
  2711. ssize_t ret;
  2712. size_t count = iov_length(iov, nr_segs);
  2713. int overwrite = 0;
  2714. get_block_t *get_block_func = NULL;
  2715. int dio_flags = 0;
  2716. loff_t final_size = offset + count;
  2717. ext4_io_end_t *io_end = NULL;
  2718. /* Use the old path for reads and writes beyond i_size. */
  2719. if (rw != WRITE || final_size > inode->i_size)
  2720. return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2721. BUG_ON(iocb->private == NULL);
  2722. /* If we do a overwrite dio, i_mutex locking can be released */
  2723. overwrite = *((int *)iocb->private);
  2724. if (overwrite) {
  2725. atomic_inc(&inode->i_dio_count);
  2726. down_read(&EXT4_I(inode)->i_data_sem);
  2727. mutex_unlock(&inode->i_mutex);
  2728. }
  2729. /*
  2730. * We could direct write to holes and fallocate.
  2731. *
  2732. * Allocated blocks to fill the hole are marked as
  2733. * uninitialized to prevent parallel buffered read to expose
  2734. * the stale data before DIO complete the data IO.
  2735. *
  2736. * As to previously fallocated extents, ext4 get_block will
  2737. * just simply mark the buffer mapped but still keep the
  2738. * extents uninitialized.
  2739. *
  2740. * For non AIO case, we will convert those unwritten extents
  2741. * to written after return back from blockdev_direct_IO.
  2742. *
  2743. * For async DIO, the conversion needs to be deferred when the
  2744. * IO is completed. The ext4 end_io callback function will be
  2745. * called to take care of the conversion work. Here for async
  2746. * case, we allocate an io_end structure to hook to the iocb.
  2747. */
  2748. iocb->private = NULL;
  2749. ext4_inode_aio_set(inode, NULL);
  2750. if (!is_sync_kiocb(iocb)) {
  2751. io_end = ext4_init_io_end(inode, GFP_NOFS);
  2752. if (!io_end) {
  2753. ret = -ENOMEM;
  2754. goto retake_lock;
  2755. }
  2756. io_end->flag |= EXT4_IO_END_DIRECT;
  2757. /*
  2758. * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
  2759. */
  2760. iocb->private = ext4_get_io_end(io_end);
  2761. /*
  2762. * we save the io structure for current async direct
  2763. * IO, so that later ext4_map_blocks() could flag the
  2764. * io structure whether there is a unwritten extents
  2765. * needs to be converted when IO is completed.
  2766. */
  2767. ext4_inode_aio_set(inode, io_end);
  2768. }
  2769. if (overwrite) {
  2770. get_block_func = ext4_get_block_write_nolock;
  2771. } else {
  2772. get_block_func = ext4_get_block_write;
  2773. dio_flags = DIO_LOCKING;
  2774. }
  2775. ret = __blockdev_direct_IO(rw, iocb, inode,
  2776. inode->i_sb->s_bdev, iov,
  2777. offset, nr_segs,
  2778. get_block_func,
  2779. ext4_end_io_dio,
  2780. NULL,
  2781. dio_flags);
  2782. /*
  2783. * Put our reference to io_end. This can free the io_end structure e.g.
  2784. * in sync IO case or in case of error. It can even perform extent
  2785. * conversion if all bios we submitted finished before we got here.
  2786. * Note that in that case iocb->private can be already set to NULL
  2787. * here.
  2788. */
  2789. if (io_end) {
  2790. ext4_inode_aio_set(inode, NULL);
  2791. ext4_put_io_end(io_end);
  2792. /*
  2793. * When no IO was submitted ext4_end_io_dio() was not
  2794. * called so we have to put iocb's reference.
  2795. */
  2796. if (ret <= 0 && ret != -EIOCBQUEUED && iocb->private) {
  2797. WARN_ON(iocb->private != io_end);
  2798. WARN_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
  2799. WARN_ON(io_end->iocb);
  2800. /*
  2801. * Generic code already did inode_dio_done() so we
  2802. * have to clear EXT4_IO_END_DIRECT to not do it for
  2803. * the second time.
  2804. */
  2805. io_end->flag = 0;
  2806. ext4_put_io_end(io_end);
  2807. iocb->private = NULL;
  2808. }
  2809. }
  2810. if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
  2811. EXT4_STATE_DIO_UNWRITTEN)) {
  2812. int err;
  2813. /*
  2814. * for non AIO case, since the IO is already
  2815. * completed, we could do the conversion right here
  2816. */
  2817. err = ext4_convert_unwritten_extents(NULL, inode,
  2818. offset, ret);
  2819. if (err < 0)
  2820. ret = err;
  2821. ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  2822. }
  2823. retake_lock:
  2824. /* take i_mutex locking again if we do a ovewrite dio */
  2825. if (overwrite) {
  2826. inode_dio_done(inode);
  2827. up_read(&EXT4_I(inode)->i_data_sem);
  2828. mutex_lock(&inode->i_mutex);
  2829. }
  2830. return ret;
  2831. }
  2832. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  2833. const struct iovec *iov, loff_t offset,
  2834. unsigned long nr_segs)
  2835. {
  2836. struct file *file = iocb->ki_filp;
  2837. struct inode *inode = file->f_mapping->host;
  2838. ssize_t ret;
  2839. /*
  2840. * If we are doing data journalling we don't support O_DIRECT
  2841. */
  2842. if (ext4_should_journal_data(inode))
  2843. return 0;
  2844. /* Let buffer I/O handle the inline data case. */
  2845. if (ext4_has_inline_data(inode))
  2846. return 0;
  2847. trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
  2848. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  2849. ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
  2850. else
  2851. ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2852. trace_ext4_direct_IO_exit(inode, offset,
  2853. iov_length(iov, nr_segs), rw, ret);
  2854. return ret;
  2855. }
  2856. /*
  2857. * Pages can be marked dirty completely asynchronously from ext4's journalling
  2858. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  2859. * much here because ->set_page_dirty is called under VFS locks. The page is
  2860. * not necessarily locked.
  2861. *
  2862. * We cannot just dirty the page and leave attached buffers clean, because the
  2863. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  2864. * or jbddirty because all the journalling code will explode.
  2865. *
  2866. * So what we do is to mark the page "pending dirty" and next time writepage
  2867. * is called, propagate that into the buffers appropriately.
  2868. */
  2869. static int ext4_journalled_set_page_dirty(struct page *page)
  2870. {
  2871. SetPageChecked(page);
  2872. return __set_page_dirty_nobuffers(page);
  2873. }
  2874. static const struct address_space_operations ext4_aops = {
  2875. .readpage = ext4_readpage,
  2876. .readpages = ext4_readpages,
  2877. .writepage = ext4_writepage,
  2878. .write_begin = ext4_write_begin,
  2879. .write_end = ext4_write_end,
  2880. .bmap = ext4_bmap,
  2881. .invalidatepage = ext4_invalidatepage,
  2882. .releasepage = ext4_releasepage,
  2883. .direct_IO = ext4_direct_IO,
  2884. .migratepage = buffer_migrate_page,
  2885. .is_partially_uptodate = block_is_partially_uptodate,
  2886. .error_remove_page = generic_error_remove_page,
  2887. };
  2888. static const struct address_space_operations ext4_journalled_aops = {
  2889. .readpage = ext4_readpage,
  2890. .readpages = ext4_readpages,
  2891. .writepage = ext4_writepage,
  2892. .write_begin = ext4_write_begin,
  2893. .write_end = ext4_journalled_write_end,
  2894. .set_page_dirty = ext4_journalled_set_page_dirty,
  2895. .bmap = ext4_bmap,
  2896. .invalidatepage = ext4_journalled_invalidatepage,
  2897. .releasepage = ext4_releasepage,
  2898. .direct_IO = ext4_direct_IO,
  2899. .is_partially_uptodate = block_is_partially_uptodate,
  2900. .error_remove_page = generic_error_remove_page,
  2901. };
  2902. static const struct address_space_operations ext4_da_aops = {
  2903. .readpage = ext4_readpage,
  2904. .readpages = ext4_readpages,
  2905. .writepage = ext4_writepage,
  2906. .writepages = ext4_da_writepages,
  2907. .write_begin = ext4_da_write_begin,
  2908. .write_end = ext4_da_write_end,
  2909. .bmap = ext4_bmap,
  2910. .invalidatepage = ext4_da_invalidatepage,
  2911. .releasepage = ext4_releasepage,
  2912. .direct_IO = ext4_direct_IO,
  2913. .migratepage = buffer_migrate_page,
  2914. .is_partially_uptodate = block_is_partially_uptodate,
  2915. .error_remove_page = generic_error_remove_page,
  2916. };
  2917. void ext4_set_aops(struct inode *inode)
  2918. {
  2919. switch (ext4_inode_journal_mode(inode)) {
  2920. case EXT4_INODE_ORDERED_DATA_MODE:
  2921. ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
  2922. break;
  2923. case EXT4_INODE_WRITEBACK_DATA_MODE:
  2924. ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
  2925. break;
  2926. case EXT4_INODE_JOURNAL_DATA_MODE:
  2927. inode->i_mapping->a_ops = &ext4_journalled_aops;
  2928. return;
  2929. default:
  2930. BUG();
  2931. }
  2932. if (test_opt(inode->i_sb, DELALLOC))
  2933. inode->i_mapping->a_ops = &ext4_da_aops;
  2934. else
  2935. inode->i_mapping->a_ops = &ext4_aops;
  2936. }
  2937. /*
  2938. * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
  2939. * up to the end of the block which corresponds to `from'.
  2940. * This required during truncate. We need to physically zero the tail end
  2941. * of that block so it doesn't yield old data if the file is later grown.
  2942. */
  2943. int ext4_block_truncate_page(handle_t *handle,
  2944. struct address_space *mapping, loff_t from)
  2945. {
  2946. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2947. unsigned length;
  2948. unsigned blocksize;
  2949. struct inode *inode = mapping->host;
  2950. blocksize = inode->i_sb->s_blocksize;
  2951. length = blocksize - (offset & (blocksize - 1));
  2952. return ext4_block_zero_page_range(handle, mapping, from, length);
  2953. }
  2954. /*
  2955. * ext4_block_zero_page_range() zeros out a mapping of length 'length'
  2956. * starting from file offset 'from'. The range to be zero'd must
  2957. * be contained with in one block. If the specified range exceeds
  2958. * the end of the block it will be shortened to end of the block
  2959. * that cooresponds to 'from'
  2960. */
  2961. int ext4_block_zero_page_range(handle_t *handle,
  2962. struct address_space *mapping, loff_t from, loff_t length)
  2963. {
  2964. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  2965. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2966. unsigned blocksize, max, pos;
  2967. ext4_lblk_t iblock;
  2968. struct inode *inode = mapping->host;
  2969. struct buffer_head *bh;
  2970. struct page *page;
  2971. int err = 0;
  2972. page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
  2973. mapping_gfp_mask(mapping) & ~__GFP_FS);
  2974. if (!page)
  2975. return -ENOMEM;
  2976. blocksize = inode->i_sb->s_blocksize;
  2977. max = blocksize - (offset & (blocksize - 1));
  2978. /*
  2979. * correct length if it does not fall between
  2980. * 'from' and the end of the block
  2981. */
  2982. if (length > max || length < 0)
  2983. length = max;
  2984. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  2985. if (!page_has_buffers(page))
  2986. create_empty_buffers(page, blocksize, 0);
  2987. /* Find the buffer that contains "offset" */
  2988. bh = page_buffers(page);
  2989. pos = blocksize;
  2990. while (offset >= pos) {
  2991. bh = bh->b_this_page;
  2992. iblock++;
  2993. pos += blocksize;
  2994. }
  2995. err = 0;
  2996. if (buffer_freed(bh)) {
  2997. BUFFER_TRACE(bh, "freed: skip");
  2998. goto unlock;
  2999. }
  3000. if (!buffer_mapped(bh)) {
  3001. BUFFER_TRACE(bh, "unmapped");
  3002. ext4_get_block(inode, iblock, bh, 0);
  3003. /* unmapped? It's a hole - nothing to do */
  3004. if (!buffer_mapped(bh)) {
  3005. BUFFER_TRACE(bh, "still unmapped");
  3006. goto unlock;
  3007. }
  3008. }
  3009. /* Ok, it's mapped. Make sure it's up-to-date */
  3010. if (PageUptodate(page))
  3011. set_buffer_uptodate(bh);
  3012. if (!buffer_uptodate(bh)) {
  3013. err = -EIO;
  3014. ll_rw_block(READ, 1, &bh);
  3015. wait_on_buffer(bh);
  3016. /* Uhhuh. Read error. Complain and punt. */
  3017. if (!buffer_uptodate(bh))
  3018. goto unlock;
  3019. }
  3020. if (ext4_should_journal_data(inode)) {
  3021. BUFFER_TRACE(bh, "get write access");
  3022. err = ext4_journal_get_write_access(handle, bh);
  3023. if (err)
  3024. goto unlock;
  3025. }
  3026. zero_user(page, offset, length);
  3027. BUFFER_TRACE(bh, "zeroed end of block");
  3028. err = 0;
  3029. if (ext4_should_journal_data(inode)) {
  3030. err = ext4_handle_dirty_metadata(handle, inode, bh);
  3031. } else {
  3032. mark_buffer_dirty(bh);
  3033. if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE))
  3034. err = ext4_jbd2_file_inode(handle, inode);
  3035. }
  3036. unlock:
  3037. unlock_page(page);
  3038. page_cache_release(page);
  3039. return err;
  3040. }
  3041. int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
  3042. loff_t lstart, loff_t length)
  3043. {
  3044. struct super_block *sb = inode->i_sb;
  3045. struct address_space *mapping = inode->i_mapping;
  3046. unsigned partial = lstart & (sb->s_blocksize - 1);
  3047. ext4_fsblk_t start, end;
  3048. loff_t byte_end = (lstart + length - 1);
  3049. int err = 0;
  3050. start = lstart >> sb->s_blocksize_bits;
  3051. end = byte_end >> sb->s_blocksize_bits;
  3052. /* Handle partial zero within the single block */
  3053. if (start == end) {
  3054. err = ext4_block_zero_page_range(handle, mapping,
  3055. lstart, length);
  3056. return err;
  3057. }
  3058. /* Handle partial zero out on the start of the range */
  3059. if (partial) {
  3060. err = ext4_block_zero_page_range(handle, mapping,
  3061. lstart, sb->s_blocksize);
  3062. if (err)
  3063. return err;
  3064. }
  3065. /* Handle partial zero out on the end of the range */
  3066. partial = byte_end & (sb->s_blocksize - 1);
  3067. if (partial != sb->s_blocksize - 1)
  3068. err = ext4_block_zero_page_range(handle, mapping,
  3069. byte_end - partial,
  3070. partial + 1);
  3071. return err;
  3072. }
  3073. int ext4_can_truncate(struct inode *inode)
  3074. {
  3075. if (S_ISREG(inode->i_mode))
  3076. return 1;
  3077. if (S_ISDIR(inode->i_mode))
  3078. return 1;
  3079. if (S_ISLNK(inode->i_mode))
  3080. return !ext4_inode_is_fast_symlink(inode);
  3081. return 0;
  3082. }
  3083. /*
  3084. * ext4_punch_hole: punches a hole in a file by releaseing the blocks
  3085. * associated with the given offset and length
  3086. *
  3087. * @inode: File inode
  3088. * @offset: The offset where the hole will begin
  3089. * @len: The length of the hole
  3090. *
  3091. * Returns: 0 on success or negative on failure
  3092. */
  3093. int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
  3094. {
  3095. struct inode *inode = file_inode(file);
  3096. struct super_block *sb = inode->i_sb;
  3097. ext4_lblk_t first_block, stop_block;
  3098. struct address_space *mapping = inode->i_mapping;
  3099. loff_t first_block_offset, last_block_offset;
  3100. handle_t *handle;
  3101. unsigned int credits;
  3102. int ret = 0;
  3103. if (!S_ISREG(inode->i_mode))
  3104. return -EOPNOTSUPP;
  3105. if (EXT4_SB(sb)->s_cluster_ratio > 1) {
  3106. /* TODO: Add support for bigalloc file systems */
  3107. return -EOPNOTSUPP;
  3108. }
  3109. trace_ext4_punch_hole(inode, offset, length);
  3110. /*
  3111. * Write out all dirty pages to avoid race conditions
  3112. * Then release them.
  3113. */
  3114. if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  3115. ret = filemap_write_and_wait_range(mapping, offset,
  3116. offset + length - 1);
  3117. if (ret)
  3118. return ret;
  3119. }
  3120. mutex_lock(&inode->i_mutex);
  3121. /* It's not possible punch hole on append only file */
  3122. if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
  3123. ret = -EPERM;
  3124. goto out_mutex;
  3125. }
  3126. if (IS_SWAPFILE(inode)) {
  3127. ret = -ETXTBSY;
  3128. goto out_mutex;
  3129. }
  3130. /* No need to punch hole beyond i_size */
  3131. if (offset >= inode->i_size)
  3132. goto out_mutex;
  3133. /*
  3134. * If the hole extends beyond i_size, set the hole
  3135. * to end after the page that contains i_size
  3136. */
  3137. if (offset + length > inode->i_size) {
  3138. length = inode->i_size +
  3139. PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
  3140. offset;
  3141. }
  3142. first_block_offset = round_up(offset, sb->s_blocksize);
  3143. last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
  3144. /* Now release the pages and zero block aligned part of pages*/
  3145. if (last_block_offset > first_block_offset)
  3146. truncate_pagecache_range(inode, first_block_offset,
  3147. last_block_offset);
  3148. /* Wait all existing dio workers, newcomers will block on i_mutex */
  3149. ext4_inode_block_unlocked_dio(inode);
  3150. ret = ext4_flush_unwritten_io(inode);
  3151. if (ret)
  3152. goto out_dio;
  3153. inode_dio_wait(inode);
  3154. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3155. credits = ext4_writepage_trans_blocks(inode);
  3156. else
  3157. credits = ext4_blocks_for_truncate(inode);
  3158. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3159. if (IS_ERR(handle)) {
  3160. ret = PTR_ERR(handle);
  3161. ext4_std_error(sb, ret);
  3162. goto out_dio;
  3163. }
  3164. ret = ext4_zero_partial_blocks(handle, inode, offset,
  3165. length);
  3166. if (ret)
  3167. goto out_stop;
  3168. first_block = (offset + sb->s_blocksize - 1) >>
  3169. EXT4_BLOCK_SIZE_BITS(sb);
  3170. stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
  3171. /* If there are no blocks to remove, return now */
  3172. if (first_block >= stop_block)
  3173. goto out_stop;
  3174. down_write(&EXT4_I(inode)->i_data_sem);
  3175. ext4_discard_preallocations(inode);
  3176. ret = ext4_es_remove_extent(inode, first_block,
  3177. stop_block - first_block);
  3178. if (ret) {
  3179. up_write(&EXT4_I(inode)->i_data_sem);
  3180. goto out_stop;
  3181. }
  3182. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3183. ret = ext4_ext_remove_space(inode, first_block,
  3184. stop_block - 1);
  3185. else
  3186. ret = ext4_free_hole_blocks(handle, inode, first_block,
  3187. stop_block);
  3188. ext4_discard_preallocations(inode);
  3189. up_write(&EXT4_I(inode)->i_data_sem);
  3190. if (IS_SYNC(inode))
  3191. ext4_handle_sync(handle);
  3192. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3193. ext4_mark_inode_dirty(handle, inode);
  3194. out_stop:
  3195. ext4_journal_stop(handle);
  3196. out_dio:
  3197. ext4_inode_resume_unlocked_dio(inode);
  3198. out_mutex:
  3199. mutex_unlock(&inode->i_mutex);
  3200. return ret;
  3201. }
  3202. /*
  3203. * ext4_truncate()
  3204. *
  3205. * We block out ext4_get_block() block instantiations across the entire
  3206. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3207. * simultaneously on behalf of the same inode.
  3208. *
  3209. * As we work through the truncate and commit bits of it to the journal there
  3210. * is one core, guiding principle: the file's tree must always be consistent on
  3211. * disk. We must be able to restart the truncate after a crash.
  3212. *
  3213. * The file's tree may be transiently inconsistent in memory (although it
  3214. * probably isn't), but whenever we close off and commit a journal transaction,
  3215. * the contents of (the filesystem + the journal) must be consistent and
  3216. * restartable. It's pretty simple, really: bottom up, right to left (although
  3217. * left-to-right works OK too).
  3218. *
  3219. * Note that at recovery time, journal replay occurs *before* the restart of
  3220. * truncate against the orphan inode list.
  3221. *
  3222. * The committed inode has the new, desired i_size (which is the same as
  3223. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3224. * that this inode's truncate did not complete and it will again call
  3225. * ext4_truncate() to have another go. So there will be instantiated blocks
  3226. * to the right of the truncation point in a crashed ext4 filesystem. But
  3227. * that's fine - as long as they are linked from the inode, the post-crash
  3228. * ext4_truncate() run will find them and release them.
  3229. */
  3230. void ext4_truncate(struct inode *inode)
  3231. {
  3232. struct ext4_inode_info *ei = EXT4_I(inode);
  3233. unsigned int credits;
  3234. handle_t *handle;
  3235. struct address_space *mapping = inode->i_mapping;
  3236. /*
  3237. * There is a possibility that we're either freeing the inode
  3238. * or it completely new indode. In those cases we might not
  3239. * have i_mutex locked because it's not necessary.
  3240. */
  3241. if (!(inode->i_state & (I_NEW|I_FREEING)))
  3242. WARN_ON(!mutex_is_locked(&inode->i_mutex));
  3243. trace_ext4_truncate_enter(inode);
  3244. if (!ext4_can_truncate(inode))
  3245. return;
  3246. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3247. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3248. ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  3249. if (ext4_has_inline_data(inode)) {
  3250. int has_inline = 1;
  3251. ext4_inline_data_truncate(inode, &has_inline);
  3252. if (has_inline)
  3253. return;
  3254. }
  3255. /*
  3256. * finish any pending end_io work so we won't run the risk of
  3257. * converting any truncated blocks to initialized later
  3258. */
  3259. ext4_flush_unwritten_io(inode);
  3260. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3261. credits = ext4_writepage_trans_blocks(inode);
  3262. else
  3263. credits = ext4_blocks_for_truncate(inode);
  3264. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
  3265. if (IS_ERR(handle)) {
  3266. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  3267. return;
  3268. }
  3269. if (inode->i_size & (inode->i_sb->s_blocksize - 1))
  3270. ext4_block_truncate_page(handle, mapping, inode->i_size);
  3271. /*
  3272. * We add the inode to the orphan list, so that if this
  3273. * truncate spans multiple transactions, and we crash, we will
  3274. * resume the truncate when the filesystem recovers. It also
  3275. * marks the inode dirty, to catch the new size.
  3276. *
  3277. * Implication: the file must always be in a sane, consistent
  3278. * truncatable state while each transaction commits.
  3279. */
  3280. if (ext4_orphan_add(handle, inode))
  3281. goto out_stop;
  3282. down_write(&EXT4_I(inode)->i_data_sem);
  3283. ext4_discard_preallocations(inode);
  3284. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3285. ext4_ext_truncate(handle, inode);
  3286. else
  3287. ext4_ind_truncate(handle, inode);
  3288. up_write(&ei->i_data_sem);
  3289. if (IS_SYNC(inode))
  3290. ext4_handle_sync(handle);
  3291. out_stop:
  3292. /*
  3293. * If this was a simple ftruncate() and the file will remain alive,
  3294. * then we need to clear up the orphan record which we created above.
  3295. * However, if this was a real unlink then we were called by
  3296. * ext4_delete_inode(), and we allow that function to clean up the
  3297. * orphan info for us.
  3298. */
  3299. if (inode->i_nlink)
  3300. ext4_orphan_del(handle, inode);
  3301. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3302. ext4_mark_inode_dirty(handle, inode);
  3303. ext4_journal_stop(handle);
  3304. trace_ext4_truncate_exit(inode);
  3305. }
  3306. /*
  3307. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3308. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3309. * data in memory that is needed to recreate the on-disk version of this
  3310. * inode.
  3311. */
  3312. static int __ext4_get_inode_loc(struct inode *inode,
  3313. struct ext4_iloc *iloc, int in_mem)
  3314. {
  3315. struct ext4_group_desc *gdp;
  3316. struct buffer_head *bh;
  3317. struct super_block *sb = inode->i_sb;
  3318. ext4_fsblk_t block;
  3319. int inodes_per_block, inode_offset;
  3320. iloc->bh = NULL;
  3321. if (!ext4_valid_inum(sb, inode->i_ino))
  3322. return -EIO;
  3323. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3324. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  3325. if (!gdp)
  3326. return -EIO;
  3327. /*
  3328. * Figure out the offset within the block group inode table
  3329. */
  3330. inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  3331. inode_offset = ((inode->i_ino - 1) %
  3332. EXT4_INODES_PER_GROUP(sb));
  3333. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  3334. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  3335. bh = sb_getblk(sb, block);
  3336. if (unlikely(!bh))
  3337. return -ENOMEM;
  3338. if (!buffer_uptodate(bh)) {
  3339. lock_buffer(bh);
  3340. /*
  3341. * If the buffer has the write error flag, we have failed
  3342. * to write out another inode in the same block. In this
  3343. * case, we don't have to read the block because we may
  3344. * read the old inode data successfully.
  3345. */
  3346. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3347. set_buffer_uptodate(bh);
  3348. if (buffer_uptodate(bh)) {
  3349. /* someone brought it uptodate while we waited */
  3350. unlock_buffer(bh);
  3351. goto has_buffer;
  3352. }
  3353. /*
  3354. * If we have all information of the inode in memory and this
  3355. * is the only valid inode in the block, we need not read the
  3356. * block.
  3357. */
  3358. if (in_mem) {
  3359. struct buffer_head *bitmap_bh;
  3360. int i, start;
  3361. start = inode_offset & ~(inodes_per_block - 1);
  3362. /* Is the inode bitmap in cache? */
  3363. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  3364. if (unlikely(!bitmap_bh))
  3365. goto make_io;
  3366. /*
  3367. * If the inode bitmap isn't in cache then the
  3368. * optimisation may end up performing two reads instead
  3369. * of one, so skip it.
  3370. */
  3371. if (!buffer_uptodate(bitmap_bh)) {
  3372. brelse(bitmap_bh);
  3373. goto make_io;
  3374. }
  3375. for (i = start; i < start + inodes_per_block; i++) {
  3376. if (i == inode_offset)
  3377. continue;
  3378. if (ext4_test_bit(i, bitmap_bh->b_data))
  3379. break;
  3380. }
  3381. brelse(bitmap_bh);
  3382. if (i == start + inodes_per_block) {
  3383. /* all other inodes are free, so skip I/O */
  3384. memset(bh->b_data, 0, bh->b_size);
  3385. set_buffer_uptodate(bh);
  3386. unlock_buffer(bh);
  3387. goto has_buffer;
  3388. }
  3389. }
  3390. make_io:
  3391. /*
  3392. * If we need to do any I/O, try to pre-readahead extra
  3393. * blocks from the inode table.
  3394. */
  3395. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  3396. ext4_fsblk_t b, end, table;
  3397. unsigned num;
  3398. __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
  3399. table = ext4_inode_table(sb, gdp);
  3400. /* s_inode_readahead_blks is always a power of 2 */
  3401. b = block & ~((ext4_fsblk_t) ra_blks - 1);
  3402. if (table > b)
  3403. b = table;
  3404. end = b + ra_blks;
  3405. num = EXT4_INODES_PER_GROUP(sb);
  3406. if (ext4_has_group_desc_csum(sb))
  3407. num -= ext4_itable_unused_count(sb, gdp);
  3408. table += num / inodes_per_block;
  3409. if (end > table)
  3410. end = table;
  3411. while (b <= end)
  3412. sb_breadahead(sb, b++);
  3413. }
  3414. /*
  3415. * There are other valid inodes in the buffer, this inode
  3416. * has in-inode xattrs, or we don't have this inode in memory.
  3417. * Read the block from disk.
  3418. */
  3419. trace_ext4_load_inode(inode);
  3420. get_bh(bh);
  3421. bh->b_end_io = end_buffer_read_sync;
  3422. submit_bh(READ | REQ_META | REQ_PRIO, bh);
  3423. wait_on_buffer(bh);
  3424. if (!buffer_uptodate(bh)) {
  3425. EXT4_ERROR_INODE_BLOCK(inode, block,
  3426. "unable to read itable block");
  3427. brelse(bh);
  3428. return -EIO;
  3429. }
  3430. }
  3431. has_buffer:
  3432. iloc->bh = bh;
  3433. return 0;
  3434. }
  3435. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3436. {
  3437. /* We have all inode data except xattrs in memory here. */
  3438. return __ext4_get_inode_loc(inode, iloc,
  3439. !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
  3440. }
  3441. void ext4_set_inode_flags(struct inode *inode)
  3442. {
  3443. unsigned int flags = EXT4_I(inode)->i_flags;
  3444. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  3445. if (flags & EXT4_SYNC_FL)
  3446. inode->i_flags |= S_SYNC;
  3447. if (flags & EXT4_APPEND_FL)
  3448. inode->i_flags |= S_APPEND;
  3449. if (flags & EXT4_IMMUTABLE_FL)
  3450. inode->i_flags |= S_IMMUTABLE;
  3451. if (flags & EXT4_NOATIME_FL)
  3452. inode->i_flags |= S_NOATIME;
  3453. if (flags & EXT4_DIRSYNC_FL)
  3454. inode->i_flags |= S_DIRSYNC;
  3455. }
  3456. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3457. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3458. {
  3459. unsigned int vfs_fl;
  3460. unsigned long old_fl, new_fl;
  3461. do {
  3462. vfs_fl = ei->vfs_inode.i_flags;
  3463. old_fl = ei->i_flags;
  3464. new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3465. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
  3466. EXT4_DIRSYNC_FL);
  3467. if (vfs_fl & S_SYNC)
  3468. new_fl |= EXT4_SYNC_FL;
  3469. if (vfs_fl & S_APPEND)
  3470. new_fl |= EXT4_APPEND_FL;
  3471. if (vfs_fl & S_IMMUTABLE)
  3472. new_fl |= EXT4_IMMUTABLE_FL;
  3473. if (vfs_fl & S_NOATIME)
  3474. new_fl |= EXT4_NOATIME_FL;
  3475. if (vfs_fl & S_DIRSYNC)
  3476. new_fl |= EXT4_DIRSYNC_FL;
  3477. } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
  3478. }
  3479. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3480. struct ext4_inode_info *ei)
  3481. {
  3482. blkcnt_t i_blocks ;
  3483. struct inode *inode = &(ei->vfs_inode);
  3484. struct super_block *sb = inode->i_sb;
  3485. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3486. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  3487. /* we are using combined 48 bit field */
  3488. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3489. le32_to_cpu(raw_inode->i_blocks_lo);
  3490. if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
  3491. /* i_blocks represent file system block size */
  3492. return i_blocks << (inode->i_blkbits - 9);
  3493. } else {
  3494. return i_blocks;
  3495. }
  3496. } else {
  3497. return le32_to_cpu(raw_inode->i_blocks_lo);
  3498. }
  3499. }
  3500. static inline void ext4_iget_extra_inode(struct inode *inode,
  3501. struct ext4_inode *raw_inode,
  3502. struct ext4_inode_info *ei)
  3503. {
  3504. __le32 *magic = (void *)raw_inode +
  3505. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
  3506. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
  3507. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  3508. ext4_find_inline_data_nolock(inode);
  3509. } else
  3510. EXT4_I(inode)->i_inline_off = 0;
  3511. }
  3512. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3513. {
  3514. struct ext4_iloc iloc;
  3515. struct ext4_inode *raw_inode;
  3516. struct ext4_inode_info *ei;
  3517. struct inode *inode;
  3518. journal_t *journal = EXT4_SB(sb)->s_journal;
  3519. long ret;
  3520. int block;
  3521. uid_t i_uid;
  3522. gid_t i_gid;
  3523. inode = iget_locked(sb, ino);
  3524. if (!inode)
  3525. return ERR_PTR(-ENOMEM);
  3526. if (!(inode->i_state & I_NEW))
  3527. return inode;
  3528. ei = EXT4_I(inode);
  3529. iloc.bh = NULL;
  3530. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3531. if (ret < 0)
  3532. goto bad_inode;
  3533. raw_inode = ext4_raw_inode(&iloc);
  3534. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3535. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3536. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3537. EXT4_INODE_SIZE(inode->i_sb)) {
  3538. EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
  3539. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
  3540. EXT4_INODE_SIZE(inode->i_sb));
  3541. ret = -EIO;
  3542. goto bad_inode;
  3543. }
  3544. } else
  3545. ei->i_extra_isize = 0;
  3546. /* Precompute checksum seed for inode metadata */
  3547. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3548. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  3549. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3550. __u32 csum;
  3551. __le32 inum = cpu_to_le32(inode->i_ino);
  3552. __le32 gen = raw_inode->i_generation;
  3553. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  3554. sizeof(inum));
  3555. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  3556. sizeof(gen));
  3557. }
  3558. if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
  3559. EXT4_ERROR_INODE(inode, "checksum invalid");
  3560. ret = -EIO;
  3561. goto bad_inode;
  3562. }
  3563. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3564. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3565. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3566. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3567. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3568. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3569. }
  3570. i_uid_write(inode, i_uid);
  3571. i_gid_write(inode, i_gid);
  3572. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  3573. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  3574. ei->i_inline_off = 0;
  3575. ei->i_dir_start_lookup = 0;
  3576. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3577. /* We now have enough fields to check if the inode was active or not.
  3578. * This is needed because nfsd might try to access dead inodes
  3579. * the test is that same one that e2fsck uses
  3580. * NeilBrown 1999oct15
  3581. */
  3582. if (inode->i_nlink == 0) {
  3583. if ((inode->i_mode == 0 ||
  3584. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
  3585. ino != EXT4_BOOT_LOADER_INO) {
  3586. /* this inode is deleted */
  3587. ret = -ESTALE;
  3588. goto bad_inode;
  3589. }
  3590. /* The only unlinked inodes we let through here have
  3591. * valid i_mode and are being read by the orphan
  3592. * recovery code: that's fine, we're about to complete
  3593. * the process of deleting those.
  3594. * OR it is the EXT4_BOOT_LOADER_INO which is
  3595. * not initialized on a new filesystem. */
  3596. }
  3597. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  3598. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  3599. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  3600. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
  3601. ei->i_file_acl |=
  3602. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  3603. inode->i_size = ext4_isize(raw_inode);
  3604. ei->i_disksize = inode->i_size;
  3605. #ifdef CONFIG_QUOTA
  3606. ei->i_reserved_quota = 0;
  3607. #endif
  3608. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  3609. ei->i_block_group = iloc.block_group;
  3610. ei->i_last_alloc_group = ~0;
  3611. /*
  3612. * NOTE! The in-memory inode i_data array is in little-endian order
  3613. * even on big-endian machines: we do NOT byteswap the block numbers!
  3614. */
  3615. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3616. ei->i_data[block] = raw_inode->i_block[block];
  3617. INIT_LIST_HEAD(&ei->i_orphan);
  3618. /*
  3619. * Set transaction id's of transactions that have to be committed
  3620. * to finish f[data]sync. We set them to currently running transaction
  3621. * as we cannot be sure that the inode or some of its metadata isn't
  3622. * part of the transaction - the inode could have been reclaimed and
  3623. * now it is reread from disk.
  3624. */
  3625. if (journal) {
  3626. transaction_t *transaction;
  3627. tid_t tid;
  3628. read_lock(&journal->j_state_lock);
  3629. if (journal->j_running_transaction)
  3630. transaction = journal->j_running_transaction;
  3631. else
  3632. transaction = journal->j_committing_transaction;
  3633. if (transaction)
  3634. tid = transaction->t_tid;
  3635. else
  3636. tid = journal->j_commit_sequence;
  3637. read_unlock(&journal->j_state_lock);
  3638. ei->i_sync_tid = tid;
  3639. ei->i_datasync_tid = tid;
  3640. }
  3641. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3642. if (ei->i_extra_isize == 0) {
  3643. /* The extra space is currently unused. Use it. */
  3644. ei->i_extra_isize = sizeof(struct ext4_inode) -
  3645. EXT4_GOOD_OLD_INODE_SIZE;
  3646. } else {
  3647. ext4_iget_extra_inode(inode, raw_inode, ei);
  3648. }
  3649. }
  3650. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  3651. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  3652. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  3653. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  3654. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  3655. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3656. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3657. inode->i_version |=
  3658. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  3659. }
  3660. ret = 0;
  3661. if (ei->i_file_acl &&
  3662. !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
  3663. EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
  3664. ei->i_file_acl);
  3665. ret = -EIO;
  3666. goto bad_inode;
  3667. } else if (!ext4_has_inline_data(inode)) {
  3668. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  3669. if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3670. (S_ISLNK(inode->i_mode) &&
  3671. !ext4_inode_is_fast_symlink(inode))))
  3672. /* Validate extent which is part of inode */
  3673. ret = ext4_ext_check_inode(inode);
  3674. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3675. (S_ISLNK(inode->i_mode) &&
  3676. !ext4_inode_is_fast_symlink(inode))) {
  3677. /* Validate block references which are part of inode */
  3678. ret = ext4_ind_check_inode(inode);
  3679. }
  3680. }
  3681. if (ret)
  3682. goto bad_inode;
  3683. if (S_ISREG(inode->i_mode)) {
  3684. inode->i_op = &ext4_file_inode_operations;
  3685. inode->i_fop = &ext4_file_operations;
  3686. ext4_set_aops(inode);
  3687. } else if (S_ISDIR(inode->i_mode)) {
  3688. inode->i_op = &ext4_dir_inode_operations;
  3689. inode->i_fop = &ext4_dir_operations;
  3690. } else if (S_ISLNK(inode->i_mode)) {
  3691. if (ext4_inode_is_fast_symlink(inode)) {
  3692. inode->i_op = &ext4_fast_symlink_inode_operations;
  3693. nd_terminate_link(ei->i_data, inode->i_size,
  3694. sizeof(ei->i_data) - 1);
  3695. } else {
  3696. inode->i_op = &ext4_symlink_inode_operations;
  3697. ext4_set_aops(inode);
  3698. }
  3699. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  3700. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  3701. inode->i_op = &ext4_special_inode_operations;
  3702. if (raw_inode->i_block[0])
  3703. init_special_inode(inode, inode->i_mode,
  3704. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  3705. else
  3706. init_special_inode(inode, inode->i_mode,
  3707. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  3708. } else if (ino == EXT4_BOOT_LOADER_INO) {
  3709. make_bad_inode(inode);
  3710. } else {
  3711. ret = -EIO;
  3712. EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
  3713. goto bad_inode;
  3714. }
  3715. brelse(iloc.bh);
  3716. ext4_set_inode_flags(inode);
  3717. unlock_new_inode(inode);
  3718. return inode;
  3719. bad_inode:
  3720. brelse(iloc.bh);
  3721. iget_failed(inode);
  3722. return ERR_PTR(ret);
  3723. }
  3724. static int ext4_inode_blocks_set(handle_t *handle,
  3725. struct ext4_inode *raw_inode,
  3726. struct ext4_inode_info *ei)
  3727. {
  3728. struct inode *inode = &(ei->vfs_inode);
  3729. u64 i_blocks = inode->i_blocks;
  3730. struct super_block *sb = inode->i_sb;
  3731. if (i_blocks <= ~0U) {
  3732. /*
  3733. * i_blocks can be represented in a 32 bit variable
  3734. * as multiple of 512 bytes
  3735. */
  3736. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3737. raw_inode->i_blocks_high = 0;
  3738. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3739. return 0;
  3740. }
  3741. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
  3742. return -EFBIG;
  3743. if (i_blocks <= 0xffffffffffffULL) {
  3744. /*
  3745. * i_blocks can be represented in a 48 bit variable
  3746. * as multiple of 512 bytes
  3747. */
  3748. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3749. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3750. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3751. } else {
  3752. ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3753. /* i_block is stored in file system block size */
  3754. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  3755. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3756. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3757. }
  3758. return 0;
  3759. }
  3760. /*
  3761. * Post the struct inode info into an on-disk inode location in the
  3762. * buffer-cache. This gobbles the caller's reference to the
  3763. * buffer_head in the inode location struct.
  3764. *
  3765. * The caller must have write access to iloc->bh.
  3766. */
  3767. static int ext4_do_update_inode(handle_t *handle,
  3768. struct inode *inode,
  3769. struct ext4_iloc *iloc)
  3770. {
  3771. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  3772. struct ext4_inode_info *ei = EXT4_I(inode);
  3773. struct buffer_head *bh = iloc->bh;
  3774. int err = 0, rc, block;
  3775. int need_datasync = 0;
  3776. uid_t i_uid;
  3777. gid_t i_gid;
  3778. /* For fields not not tracking in the in-memory inode,
  3779. * initialise them to zero for new inodes. */
  3780. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  3781. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  3782. ext4_get_inode_flags(ei);
  3783. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  3784. i_uid = i_uid_read(inode);
  3785. i_gid = i_gid_read(inode);
  3786. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3787. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
  3788. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
  3789. /*
  3790. * Fix up interoperability with old kernels. Otherwise, old inodes get
  3791. * re-used with the upper 16 bits of the uid/gid intact
  3792. */
  3793. if (!ei->i_dtime) {
  3794. raw_inode->i_uid_high =
  3795. cpu_to_le16(high_16_bits(i_uid));
  3796. raw_inode->i_gid_high =
  3797. cpu_to_le16(high_16_bits(i_gid));
  3798. } else {
  3799. raw_inode->i_uid_high = 0;
  3800. raw_inode->i_gid_high = 0;
  3801. }
  3802. } else {
  3803. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
  3804. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
  3805. raw_inode->i_uid_high = 0;
  3806. raw_inode->i_gid_high = 0;
  3807. }
  3808. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  3809. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  3810. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  3811. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  3812. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  3813. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  3814. goto out_brelse;
  3815. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  3816. raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
  3817. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  3818. cpu_to_le32(EXT4_OS_HURD))
  3819. raw_inode->i_file_acl_high =
  3820. cpu_to_le16(ei->i_file_acl >> 32);
  3821. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  3822. if (ei->i_disksize != ext4_isize(raw_inode)) {
  3823. ext4_isize_set(raw_inode, ei->i_disksize);
  3824. need_datasync = 1;
  3825. }
  3826. if (ei->i_disksize > 0x7fffffffULL) {
  3827. struct super_block *sb = inode->i_sb;
  3828. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3829. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  3830. EXT4_SB(sb)->s_es->s_rev_level ==
  3831. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  3832. /* If this is the first large file
  3833. * created, add a flag to the superblock.
  3834. */
  3835. err = ext4_journal_get_write_access(handle,
  3836. EXT4_SB(sb)->s_sbh);
  3837. if (err)
  3838. goto out_brelse;
  3839. ext4_update_dynamic_rev(sb);
  3840. EXT4_SET_RO_COMPAT_FEATURE(sb,
  3841. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  3842. ext4_handle_sync(handle);
  3843. err = ext4_handle_dirty_super(handle, sb);
  3844. }
  3845. }
  3846. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  3847. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  3848. if (old_valid_dev(inode->i_rdev)) {
  3849. raw_inode->i_block[0] =
  3850. cpu_to_le32(old_encode_dev(inode->i_rdev));
  3851. raw_inode->i_block[1] = 0;
  3852. } else {
  3853. raw_inode->i_block[0] = 0;
  3854. raw_inode->i_block[1] =
  3855. cpu_to_le32(new_encode_dev(inode->i_rdev));
  3856. raw_inode->i_block[2] = 0;
  3857. }
  3858. } else if (!ext4_has_inline_data(inode)) {
  3859. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3860. raw_inode->i_block[block] = ei->i_data[block];
  3861. }
  3862. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  3863. if (ei->i_extra_isize) {
  3864. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3865. raw_inode->i_version_hi =
  3866. cpu_to_le32(inode->i_version >> 32);
  3867. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  3868. }
  3869. ext4_inode_csum_set(inode, raw_inode, ei);
  3870. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  3871. rc = ext4_handle_dirty_metadata(handle, NULL, bh);
  3872. if (!err)
  3873. err = rc;
  3874. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  3875. ext4_update_inode_fsync_trans(handle, inode, need_datasync);
  3876. out_brelse:
  3877. brelse(bh);
  3878. ext4_std_error(inode->i_sb, err);
  3879. return err;
  3880. }
  3881. /*
  3882. * ext4_write_inode()
  3883. *
  3884. * We are called from a few places:
  3885. *
  3886. * - Within generic_file_write() for O_SYNC files.
  3887. * Here, there will be no transaction running. We wait for any running
  3888. * transaction to commit.
  3889. *
  3890. * - Within sys_sync(), kupdate and such.
  3891. * We wait on commit, if tol to.
  3892. *
  3893. * - Within prune_icache() (PF_MEMALLOC == true)
  3894. * Here we simply return. We can't afford to block kswapd on the
  3895. * journal commit.
  3896. *
  3897. * In all cases it is actually safe for us to return without doing anything,
  3898. * because the inode has been copied into a raw inode buffer in
  3899. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  3900. * knfsd.
  3901. *
  3902. * Note that we are absolutely dependent upon all inode dirtiers doing the
  3903. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  3904. * which we are interested.
  3905. *
  3906. * It would be a bug for them to not do this. The code:
  3907. *
  3908. * mark_inode_dirty(inode)
  3909. * stuff();
  3910. * inode->i_size = expr;
  3911. *
  3912. * is in error because a kswapd-driven write_inode() could occur while
  3913. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  3914. * will no longer be on the superblock's dirty inode list.
  3915. */
  3916. int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
  3917. {
  3918. int err;
  3919. if (current->flags & PF_MEMALLOC)
  3920. return 0;
  3921. if (EXT4_SB(inode->i_sb)->s_journal) {
  3922. if (ext4_journal_current_handle()) {
  3923. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  3924. dump_stack();
  3925. return -EIO;
  3926. }
  3927. if (wbc->sync_mode != WB_SYNC_ALL)
  3928. return 0;
  3929. err = ext4_force_commit(inode->i_sb);
  3930. } else {
  3931. struct ext4_iloc iloc;
  3932. err = __ext4_get_inode_loc(inode, &iloc, 0);
  3933. if (err)
  3934. return err;
  3935. if (wbc->sync_mode == WB_SYNC_ALL)
  3936. sync_dirty_buffer(iloc.bh);
  3937. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  3938. EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
  3939. "IO error syncing inode");
  3940. err = -EIO;
  3941. }
  3942. brelse(iloc.bh);
  3943. }
  3944. return err;
  3945. }
  3946. /*
  3947. * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
  3948. * buffers that are attached to a page stradding i_size and are undergoing
  3949. * commit. In that case we have to wait for commit to finish and try again.
  3950. */
  3951. static void ext4_wait_for_tail_page_commit(struct inode *inode)
  3952. {
  3953. struct page *page;
  3954. unsigned offset;
  3955. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  3956. tid_t commit_tid = 0;
  3957. int ret;
  3958. offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
  3959. /*
  3960. * All buffers in the last page remain valid? Then there's nothing to
  3961. * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
  3962. * blocksize case
  3963. */
  3964. if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
  3965. return;
  3966. while (1) {
  3967. page = find_lock_page(inode->i_mapping,
  3968. inode->i_size >> PAGE_CACHE_SHIFT);
  3969. if (!page)
  3970. return;
  3971. ret = __ext4_journalled_invalidatepage(page, offset,
  3972. PAGE_CACHE_SIZE - offset);
  3973. unlock_page(page);
  3974. page_cache_release(page);
  3975. if (ret != -EBUSY)
  3976. return;
  3977. commit_tid = 0;
  3978. read_lock(&journal->j_state_lock);
  3979. if (journal->j_committing_transaction)
  3980. commit_tid = journal->j_committing_transaction->t_tid;
  3981. read_unlock(&journal->j_state_lock);
  3982. if (commit_tid)
  3983. jbd2_log_wait_commit(journal, commit_tid);
  3984. }
  3985. }
  3986. /*
  3987. * ext4_setattr()
  3988. *
  3989. * Called from notify_change.
  3990. *
  3991. * We want to trap VFS attempts to truncate the file as soon as
  3992. * possible. In particular, we want to make sure that when the VFS
  3993. * shrinks i_size, we put the inode on the orphan list and modify
  3994. * i_disksize immediately, so that during the subsequent flushing of
  3995. * dirty pages and freeing of disk blocks, we can guarantee that any
  3996. * commit will leave the blocks being flushed in an unused state on
  3997. * disk. (On recovery, the inode will get truncated and the blocks will
  3998. * be freed, so we have a strong guarantee that no future commit will
  3999. * leave these blocks visible to the user.)
  4000. *
  4001. * Another thing we have to assure is that if we are in ordered mode
  4002. * and inode is still attached to the committing transaction, we must
  4003. * we start writeout of all the dirty pages which are being truncated.
  4004. * This way we are sure that all the data written in the previous
  4005. * transaction are already on disk (truncate waits for pages under
  4006. * writeback).
  4007. *
  4008. * Called with inode->i_mutex down.
  4009. */
  4010. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  4011. {
  4012. struct inode *inode = dentry->d_inode;
  4013. int error, rc = 0;
  4014. int orphan = 0;
  4015. const unsigned int ia_valid = attr->ia_valid;
  4016. error = inode_change_ok(inode, attr);
  4017. if (error)
  4018. return error;
  4019. if (is_quota_modification(inode, attr))
  4020. dquot_initialize(inode);
  4021. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  4022. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  4023. handle_t *handle;
  4024. /* (user+group)*(old+new) structure, inode write (sb,
  4025. * inode block, ? - but truncate inode update has it) */
  4026. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  4027. (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
  4028. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
  4029. if (IS_ERR(handle)) {
  4030. error = PTR_ERR(handle);
  4031. goto err_out;
  4032. }
  4033. error = dquot_transfer(inode, attr);
  4034. if (error) {
  4035. ext4_journal_stop(handle);
  4036. return error;
  4037. }
  4038. /* Update corresponding info in inode so that everything is in
  4039. * one transaction */
  4040. if (attr->ia_valid & ATTR_UID)
  4041. inode->i_uid = attr->ia_uid;
  4042. if (attr->ia_valid & ATTR_GID)
  4043. inode->i_gid = attr->ia_gid;
  4044. error = ext4_mark_inode_dirty(handle, inode);
  4045. ext4_journal_stop(handle);
  4046. }
  4047. if (attr->ia_valid & ATTR_SIZE) {
  4048. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  4049. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4050. if (attr->ia_size > sbi->s_bitmap_maxbytes)
  4051. return -EFBIG;
  4052. }
  4053. }
  4054. if (S_ISREG(inode->i_mode) &&
  4055. attr->ia_valid & ATTR_SIZE &&
  4056. (attr->ia_size < inode->i_size)) {
  4057. handle_t *handle;
  4058. handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
  4059. if (IS_ERR(handle)) {
  4060. error = PTR_ERR(handle);
  4061. goto err_out;
  4062. }
  4063. if (ext4_handle_valid(handle)) {
  4064. error = ext4_orphan_add(handle, inode);
  4065. orphan = 1;
  4066. }
  4067. EXT4_I(inode)->i_disksize = attr->ia_size;
  4068. rc = ext4_mark_inode_dirty(handle, inode);
  4069. if (!error)
  4070. error = rc;
  4071. ext4_journal_stop(handle);
  4072. if (ext4_should_order_data(inode)) {
  4073. error = ext4_begin_ordered_truncate(inode,
  4074. attr->ia_size);
  4075. if (error) {
  4076. /* Do as much error cleanup as possible */
  4077. handle = ext4_journal_start(inode,
  4078. EXT4_HT_INODE, 3);
  4079. if (IS_ERR(handle)) {
  4080. ext4_orphan_del(NULL, inode);
  4081. goto err_out;
  4082. }
  4083. ext4_orphan_del(handle, inode);
  4084. orphan = 0;
  4085. ext4_journal_stop(handle);
  4086. goto err_out;
  4087. }
  4088. }
  4089. }
  4090. if (attr->ia_valid & ATTR_SIZE) {
  4091. if (attr->ia_size != inode->i_size) {
  4092. loff_t oldsize = inode->i_size;
  4093. i_size_write(inode, attr->ia_size);
  4094. /*
  4095. * Blocks are going to be removed from the inode. Wait
  4096. * for dio in flight. Temporarily disable
  4097. * dioread_nolock to prevent livelock.
  4098. */
  4099. if (orphan) {
  4100. if (!ext4_should_journal_data(inode)) {
  4101. ext4_inode_block_unlocked_dio(inode);
  4102. inode_dio_wait(inode);
  4103. ext4_inode_resume_unlocked_dio(inode);
  4104. } else
  4105. ext4_wait_for_tail_page_commit(inode);
  4106. }
  4107. /*
  4108. * Truncate pagecache after we've waited for commit
  4109. * in data=journal mode to make pages freeable.
  4110. */
  4111. truncate_pagecache(inode, oldsize, inode->i_size);
  4112. }
  4113. ext4_truncate(inode);
  4114. }
  4115. if (!rc) {
  4116. setattr_copy(inode, attr);
  4117. mark_inode_dirty(inode);
  4118. }
  4119. /*
  4120. * If the call to ext4_truncate failed to get a transaction handle at
  4121. * all, we need to clean up the in-core orphan list manually.
  4122. */
  4123. if (orphan && inode->i_nlink)
  4124. ext4_orphan_del(NULL, inode);
  4125. if (!rc && (ia_valid & ATTR_MODE))
  4126. rc = ext4_acl_chmod(inode);
  4127. err_out:
  4128. ext4_std_error(inode->i_sb, error);
  4129. if (!error)
  4130. error = rc;
  4131. return error;
  4132. }
  4133. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  4134. struct kstat *stat)
  4135. {
  4136. struct inode *inode;
  4137. unsigned long long delalloc_blocks;
  4138. inode = dentry->d_inode;
  4139. generic_fillattr(inode, stat);
  4140. /*
  4141. * We can't update i_blocks if the block allocation is delayed
  4142. * otherwise in the case of system crash before the real block
  4143. * allocation is done, we will have i_blocks inconsistent with
  4144. * on-disk file blocks.
  4145. * We always keep i_blocks updated together with real
  4146. * allocation. But to not confuse with user, stat
  4147. * will return the blocks that include the delayed allocation
  4148. * blocks for this file.
  4149. */
  4150. delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  4151. EXT4_I(inode)->i_reserved_data_blocks);
  4152. stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits-9);
  4153. return 0;
  4154. }
  4155. static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
  4156. int pextents)
  4157. {
  4158. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4159. return ext4_ind_trans_blocks(inode, lblocks);
  4160. return ext4_ext_index_trans_blocks(inode, pextents);
  4161. }
  4162. /*
  4163. * Account for index blocks, block groups bitmaps and block group
  4164. * descriptor blocks if modify datablocks and index blocks
  4165. * worse case, the indexs blocks spread over different block groups
  4166. *
  4167. * If datablocks are discontiguous, they are possible to spread over
  4168. * different block groups too. If they are contiguous, with flexbg,
  4169. * they could still across block group boundary.
  4170. *
  4171. * Also account for superblock, inode, quota and xattr blocks
  4172. */
  4173. static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
  4174. int pextents)
  4175. {
  4176. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  4177. int gdpblocks;
  4178. int idxblocks;
  4179. int ret = 0;
  4180. /*
  4181. * How many index blocks need to touch to map @lblocks logical blocks
  4182. * to @pextents physical extents?
  4183. */
  4184. idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
  4185. ret = idxblocks;
  4186. /*
  4187. * Now let's see how many group bitmaps and group descriptors need
  4188. * to account
  4189. */
  4190. groups = idxblocks + pextents;
  4191. gdpblocks = groups;
  4192. if (groups > ngroups)
  4193. groups = ngroups;
  4194. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  4195. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  4196. /* bitmaps and block group descriptor blocks */
  4197. ret += groups + gdpblocks;
  4198. /* Blocks for super block, inode, quota and xattr blocks */
  4199. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  4200. return ret;
  4201. }
  4202. /*
  4203. * Calculate the total number of credits to reserve to fit
  4204. * the modification of a single pages into a single transaction,
  4205. * which may include multiple chunks of block allocations.
  4206. *
  4207. * This could be called via ext4_write_begin()
  4208. *
  4209. * We need to consider the worse case, when
  4210. * one new block per extent.
  4211. */
  4212. int ext4_writepage_trans_blocks(struct inode *inode)
  4213. {
  4214. int bpp = ext4_journal_blocks_per_page(inode);
  4215. int ret;
  4216. ret = ext4_meta_trans_blocks(inode, bpp, bpp);
  4217. /* Account for data blocks for journalled mode */
  4218. if (ext4_should_journal_data(inode))
  4219. ret += bpp;
  4220. return ret;
  4221. }
  4222. /*
  4223. * Calculate the journal credits for a chunk of data modification.
  4224. *
  4225. * This is called from DIO, fallocate or whoever calling
  4226. * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
  4227. *
  4228. * journal buffers for data blocks are not included here, as DIO
  4229. * and fallocate do no need to journal data buffers.
  4230. */
  4231. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  4232. {
  4233. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  4234. }
  4235. /*
  4236. * The caller must have previously called ext4_reserve_inode_write().
  4237. * Give this, we know that the caller already has write access to iloc->bh.
  4238. */
  4239. int ext4_mark_iloc_dirty(handle_t *handle,
  4240. struct inode *inode, struct ext4_iloc *iloc)
  4241. {
  4242. int err = 0;
  4243. if (IS_I_VERSION(inode))
  4244. inode_inc_iversion(inode);
  4245. /* the do_update_inode consumes one bh->b_count */
  4246. get_bh(iloc->bh);
  4247. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4248. err = ext4_do_update_inode(handle, inode, iloc);
  4249. put_bh(iloc->bh);
  4250. return err;
  4251. }
  4252. /*
  4253. * On success, We end up with an outstanding reference count against
  4254. * iloc->bh. This _must_ be cleaned up later.
  4255. */
  4256. int
  4257. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4258. struct ext4_iloc *iloc)
  4259. {
  4260. int err;
  4261. err = ext4_get_inode_loc(inode, iloc);
  4262. if (!err) {
  4263. BUFFER_TRACE(iloc->bh, "get_write_access");
  4264. err = ext4_journal_get_write_access(handle, iloc->bh);
  4265. if (err) {
  4266. brelse(iloc->bh);
  4267. iloc->bh = NULL;
  4268. }
  4269. }
  4270. ext4_std_error(inode->i_sb, err);
  4271. return err;
  4272. }
  4273. /*
  4274. * Expand an inode by new_extra_isize bytes.
  4275. * Returns 0 on success or negative error number on failure.
  4276. */
  4277. static int ext4_expand_extra_isize(struct inode *inode,
  4278. unsigned int new_extra_isize,
  4279. struct ext4_iloc iloc,
  4280. handle_t *handle)
  4281. {
  4282. struct ext4_inode *raw_inode;
  4283. struct ext4_xattr_ibody_header *header;
  4284. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4285. return 0;
  4286. raw_inode = ext4_raw_inode(&iloc);
  4287. header = IHDR(inode, raw_inode);
  4288. /* No extended attributes present */
  4289. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  4290. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4291. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4292. new_extra_isize);
  4293. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4294. return 0;
  4295. }
  4296. /* try to expand with EAs present */
  4297. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4298. raw_inode, handle);
  4299. }
  4300. /*
  4301. * What we do here is to mark the in-core inode as clean with respect to inode
  4302. * dirtiness (it may still be data-dirty).
  4303. * This means that the in-core inode may be reaped by prune_icache
  4304. * without having to perform any I/O. This is a very good thing,
  4305. * because *any* task may call prune_icache - even ones which
  4306. * have a transaction open against a different journal.
  4307. *
  4308. * Is this cheating? Not really. Sure, we haven't written the
  4309. * inode out, but prune_icache isn't a user-visible syncing function.
  4310. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4311. * we start and wait on commits.
  4312. */
  4313. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4314. {
  4315. struct ext4_iloc iloc;
  4316. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4317. static unsigned int mnt_count;
  4318. int err, ret;
  4319. might_sleep();
  4320. trace_ext4_mark_inode_dirty(inode, _RET_IP_);
  4321. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4322. if (ext4_handle_valid(handle) &&
  4323. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4324. !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
  4325. /*
  4326. * We need extra buffer credits since we may write into EA block
  4327. * with this same handle. If journal_extend fails, then it will
  4328. * only result in a minor loss of functionality for that inode.
  4329. * If this is felt to be critical, then e2fsck should be run to
  4330. * force a large enough s_min_extra_isize.
  4331. */
  4332. if ((jbd2_journal_extend(handle,
  4333. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4334. ret = ext4_expand_extra_isize(inode,
  4335. sbi->s_want_extra_isize,
  4336. iloc, handle);
  4337. if (ret) {
  4338. ext4_set_inode_state(inode,
  4339. EXT4_STATE_NO_EXPAND);
  4340. if (mnt_count !=
  4341. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4342. ext4_warning(inode->i_sb,
  4343. "Unable to expand inode %lu. Delete"
  4344. " some EAs or run e2fsck.",
  4345. inode->i_ino);
  4346. mnt_count =
  4347. le16_to_cpu(sbi->s_es->s_mnt_count);
  4348. }
  4349. }
  4350. }
  4351. }
  4352. if (!err)
  4353. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  4354. return err;
  4355. }
  4356. /*
  4357. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4358. *
  4359. * We're really interested in the case where a file is being extended.
  4360. * i_size has been changed by generic_commit_write() and we thus need
  4361. * to include the updated inode in the current transaction.
  4362. *
  4363. * Also, dquot_alloc_block() will always dirty the inode when blocks
  4364. * are allocated to the file.
  4365. *
  4366. * If the inode is marked synchronous, we don't honour that here - doing
  4367. * so would cause a commit on atime updates, which we don't bother doing.
  4368. * We handle synchronous inodes at the highest possible level.
  4369. */
  4370. void ext4_dirty_inode(struct inode *inode, int flags)
  4371. {
  4372. handle_t *handle;
  4373. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  4374. if (IS_ERR(handle))
  4375. goto out;
  4376. ext4_mark_inode_dirty(handle, inode);
  4377. ext4_journal_stop(handle);
  4378. out:
  4379. return;
  4380. }
  4381. #if 0
  4382. /*
  4383. * Bind an inode's backing buffer_head into this transaction, to prevent
  4384. * it from being flushed to disk early. Unlike
  4385. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4386. * returns no iloc structure, so the caller needs to repeat the iloc
  4387. * lookup to mark the inode dirty later.
  4388. */
  4389. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4390. {
  4391. struct ext4_iloc iloc;
  4392. int err = 0;
  4393. if (handle) {
  4394. err = ext4_get_inode_loc(inode, &iloc);
  4395. if (!err) {
  4396. BUFFER_TRACE(iloc.bh, "get_write_access");
  4397. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4398. if (!err)
  4399. err = ext4_handle_dirty_metadata(handle,
  4400. NULL,
  4401. iloc.bh);
  4402. brelse(iloc.bh);
  4403. }
  4404. }
  4405. ext4_std_error(inode->i_sb, err);
  4406. return err;
  4407. }
  4408. #endif
  4409. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4410. {
  4411. journal_t *journal;
  4412. handle_t *handle;
  4413. int err;
  4414. /*
  4415. * We have to be very careful here: changing a data block's
  4416. * journaling status dynamically is dangerous. If we write a
  4417. * data block to the journal, change the status and then delete
  4418. * that block, we risk forgetting to revoke the old log record
  4419. * from the journal and so a subsequent replay can corrupt data.
  4420. * So, first we make sure that the journal is empty and that
  4421. * nobody is changing anything.
  4422. */
  4423. journal = EXT4_JOURNAL(inode);
  4424. if (!journal)
  4425. return 0;
  4426. if (is_journal_aborted(journal))
  4427. return -EROFS;
  4428. /* We have to allocate physical blocks for delalloc blocks
  4429. * before flushing journal. otherwise delalloc blocks can not
  4430. * be allocated any more. even more truncate on delalloc blocks
  4431. * could trigger BUG by flushing delalloc blocks in journal.
  4432. * There is no delalloc block in non-journal data mode.
  4433. */
  4434. if (val && test_opt(inode->i_sb, DELALLOC)) {
  4435. err = ext4_alloc_da_blocks(inode);
  4436. if (err < 0)
  4437. return err;
  4438. }
  4439. /* Wait for all existing dio workers */
  4440. ext4_inode_block_unlocked_dio(inode);
  4441. inode_dio_wait(inode);
  4442. jbd2_journal_lock_updates(journal);
  4443. /*
  4444. * OK, there are no updates running now, and all cached data is
  4445. * synced to disk. We are now in a completely consistent state
  4446. * which doesn't have anything in the journal, and we know that
  4447. * no filesystem updates are running, so it is safe to modify
  4448. * the inode's in-core data-journaling state flag now.
  4449. */
  4450. if (val)
  4451. ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4452. else {
  4453. jbd2_journal_flush(journal);
  4454. ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4455. }
  4456. ext4_set_aops(inode);
  4457. jbd2_journal_unlock_updates(journal);
  4458. ext4_inode_resume_unlocked_dio(inode);
  4459. /* Finally we can mark the inode as dirty. */
  4460. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  4461. if (IS_ERR(handle))
  4462. return PTR_ERR(handle);
  4463. err = ext4_mark_inode_dirty(handle, inode);
  4464. ext4_handle_sync(handle);
  4465. ext4_journal_stop(handle);
  4466. ext4_std_error(inode->i_sb, err);
  4467. return err;
  4468. }
  4469. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  4470. {
  4471. return !buffer_mapped(bh);
  4472. }
  4473. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4474. {
  4475. struct page *page = vmf->page;
  4476. loff_t size;
  4477. unsigned long len;
  4478. int ret;
  4479. struct file *file = vma->vm_file;
  4480. struct inode *inode = file_inode(file);
  4481. struct address_space *mapping = inode->i_mapping;
  4482. handle_t *handle;
  4483. get_block_t *get_block;
  4484. int retries = 0;
  4485. sb_start_pagefault(inode->i_sb);
  4486. file_update_time(vma->vm_file);
  4487. /* Delalloc case is easy... */
  4488. if (test_opt(inode->i_sb, DELALLOC) &&
  4489. !ext4_should_journal_data(inode) &&
  4490. !ext4_nonda_switch(inode->i_sb)) {
  4491. do {
  4492. ret = __block_page_mkwrite(vma, vmf,
  4493. ext4_da_get_block_prep);
  4494. } while (ret == -ENOSPC &&
  4495. ext4_should_retry_alloc(inode->i_sb, &retries));
  4496. goto out_ret;
  4497. }
  4498. lock_page(page);
  4499. size = i_size_read(inode);
  4500. /* Page got truncated from under us? */
  4501. if (page->mapping != mapping || page_offset(page) > size) {
  4502. unlock_page(page);
  4503. ret = VM_FAULT_NOPAGE;
  4504. goto out;
  4505. }
  4506. if (page->index == size >> PAGE_CACHE_SHIFT)
  4507. len = size & ~PAGE_CACHE_MASK;
  4508. else
  4509. len = PAGE_CACHE_SIZE;
  4510. /*
  4511. * Return if we have all the buffers mapped. This avoids the need to do
  4512. * journal_start/journal_stop which can block and take a long time
  4513. */
  4514. if (page_has_buffers(page)) {
  4515. if (!ext4_walk_page_buffers(NULL, page_buffers(page),
  4516. 0, len, NULL,
  4517. ext4_bh_unmapped)) {
  4518. /* Wait so that we don't change page under IO */
  4519. wait_for_stable_page(page);
  4520. ret = VM_FAULT_LOCKED;
  4521. goto out;
  4522. }
  4523. }
  4524. unlock_page(page);
  4525. /* OK, we need to fill the hole... */
  4526. if (ext4_should_dioread_nolock(inode))
  4527. get_block = ext4_get_block_write;
  4528. else
  4529. get_block = ext4_get_block;
  4530. retry_alloc:
  4531. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
  4532. ext4_writepage_trans_blocks(inode));
  4533. if (IS_ERR(handle)) {
  4534. ret = VM_FAULT_SIGBUS;
  4535. goto out;
  4536. }
  4537. ret = __block_page_mkwrite(vma, vmf, get_block);
  4538. if (!ret && ext4_should_journal_data(inode)) {
  4539. if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
  4540. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
  4541. unlock_page(page);
  4542. ret = VM_FAULT_SIGBUS;
  4543. ext4_journal_stop(handle);
  4544. goto out;
  4545. }
  4546. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  4547. }
  4548. ext4_journal_stop(handle);
  4549. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  4550. goto retry_alloc;
  4551. out_ret:
  4552. ret = block_page_mkwrite_return(ret);
  4553. out:
  4554. sb_end_pagefault(inode->i_sb);
  4555. return ret;
  4556. }