inode.c 146 KB

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