inode.c 150 KB

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