inode.c 138 KB

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