inode.c 148 KB

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