inode.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812
  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. if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  1771. ext4_bh_delay_or_unwritten)) {
  1772. /*
  1773. * We don't want to do block allocation, so redirty
  1774. * the page and return. We may reach here when we do
  1775. * a journal commit via journal_submit_inode_data_buffers.
  1776. * We can also reach here via shrink_page_list but it
  1777. * should never be for direct reclaim so warn if that
  1778. * happens
  1779. */
  1780. WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  1781. PF_MEMALLOC);
  1782. redirty_page_for_writepage(wbc, page);
  1783. unlock_page(page);
  1784. return 0;
  1785. }
  1786. if (PageChecked(page) && ext4_should_journal_data(inode))
  1787. /*
  1788. * It's mmapped pagecache. Add buffers and journal it. There
  1789. * doesn't seem much point in redirtying the page here.
  1790. */
  1791. return __ext4_journalled_writepage(page, len);
  1792. memset(&io_submit, 0, sizeof(io_submit));
  1793. ret = ext4_bio_write_page(&io_submit, page, len, wbc);
  1794. ext4_io_submit(&io_submit);
  1795. return ret;
  1796. }
  1797. /*
  1798. * This is called via ext4_da_writepages() to
  1799. * calculate the total number of credits to reserve to fit
  1800. * a single extent allocation into a single transaction,
  1801. * ext4_da_writpeages() will loop calling this before
  1802. * the block allocation.
  1803. */
  1804. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  1805. {
  1806. int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  1807. /*
  1808. * With non-extent format the journal credit needed to
  1809. * insert nrblocks contiguous block is dependent on
  1810. * number of contiguous block. So we will limit
  1811. * number of contiguous block to a sane value
  1812. */
  1813. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
  1814. (max_blocks > EXT4_MAX_TRANS_DATA))
  1815. max_blocks = EXT4_MAX_TRANS_DATA;
  1816. return ext4_chunk_trans_blocks(inode, max_blocks);
  1817. }
  1818. /*
  1819. * write_cache_pages_da - walk the list of dirty pages of the given
  1820. * address space and accumulate pages that need writing, and call
  1821. * mpage_da_map_and_submit to map a single contiguous memory region
  1822. * and then write them.
  1823. */
  1824. static int write_cache_pages_da(handle_t *handle,
  1825. struct address_space *mapping,
  1826. struct writeback_control *wbc,
  1827. struct mpage_da_data *mpd,
  1828. pgoff_t *done_index)
  1829. {
  1830. struct buffer_head *bh, *head;
  1831. struct inode *inode = mapping->host;
  1832. struct pagevec pvec;
  1833. unsigned int nr_pages;
  1834. sector_t logical;
  1835. pgoff_t index, end;
  1836. long nr_to_write = wbc->nr_to_write;
  1837. int i, tag, ret = 0;
  1838. memset(mpd, 0, sizeof(struct mpage_da_data));
  1839. mpd->wbc = wbc;
  1840. mpd->inode = inode;
  1841. pagevec_init(&pvec, 0);
  1842. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1843. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1844. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  1845. tag = PAGECACHE_TAG_TOWRITE;
  1846. else
  1847. tag = PAGECACHE_TAG_DIRTY;
  1848. *done_index = index;
  1849. while (index <= end) {
  1850. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  1851. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1852. if (nr_pages == 0)
  1853. return 0;
  1854. for (i = 0; i < nr_pages; i++) {
  1855. struct page *page = pvec.pages[i];
  1856. /*
  1857. * At this point, the page may be truncated or
  1858. * invalidated (changing page->mapping to NULL), or
  1859. * even swizzled back from swapper_space to tmpfs file
  1860. * mapping. However, page->index will not change
  1861. * because we have a reference on the page.
  1862. */
  1863. if (page->index > end)
  1864. goto out;
  1865. *done_index = page->index + 1;
  1866. /*
  1867. * If we can't merge this page, and we have
  1868. * accumulated an contiguous region, write it
  1869. */
  1870. if ((mpd->next_page != page->index) &&
  1871. (mpd->next_page != mpd->first_page)) {
  1872. mpage_da_map_and_submit(mpd);
  1873. goto ret_extent_tail;
  1874. }
  1875. lock_page(page);
  1876. /*
  1877. * If the page is no longer dirty, or its
  1878. * mapping no longer corresponds to inode we
  1879. * are writing (which means it has been
  1880. * truncated or invalidated), or the page is
  1881. * already under writeback and we are not
  1882. * doing a data integrity writeback, skip the page
  1883. */
  1884. if (!PageDirty(page) ||
  1885. (PageWriteback(page) &&
  1886. (wbc->sync_mode == WB_SYNC_NONE)) ||
  1887. unlikely(page->mapping != mapping)) {
  1888. unlock_page(page);
  1889. continue;
  1890. }
  1891. wait_on_page_writeback(page);
  1892. BUG_ON(PageWriteback(page));
  1893. /*
  1894. * If we have inline data and arrive here, it means that
  1895. * we will soon create the block for the 1st page, so
  1896. * we'd better clear the inline data here.
  1897. */
  1898. if (ext4_has_inline_data(inode)) {
  1899. BUG_ON(ext4_test_inode_state(inode,
  1900. EXT4_STATE_MAY_INLINE_DATA));
  1901. ext4_destroy_inline_data(handle, inode);
  1902. }
  1903. if (mpd->next_page != page->index)
  1904. mpd->first_page = page->index;
  1905. mpd->next_page = page->index + 1;
  1906. logical = (sector_t) page->index <<
  1907. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1908. /* Add all dirty buffers to mpd */
  1909. head = page_buffers(page);
  1910. bh = head;
  1911. do {
  1912. BUG_ON(buffer_locked(bh));
  1913. /*
  1914. * We need to try to allocate unmapped blocks
  1915. * in the same page. Otherwise we won't make
  1916. * progress with the page in ext4_writepage
  1917. */
  1918. if (ext4_bh_delay_or_unwritten(NULL, bh)) {
  1919. mpage_add_bh_to_extent(mpd, logical,
  1920. bh->b_state);
  1921. if (mpd->io_done)
  1922. goto ret_extent_tail;
  1923. } else if (buffer_dirty(bh) &&
  1924. buffer_mapped(bh)) {
  1925. /*
  1926. * mapped dirty buffer. We need to
  1927. * update the b_state because we look
  1928. * at b_state in mpage_da_map_blocks.
  1929. * We don't update b_size because if we
  1930. * find an unmapped buffer_head later
  1931. * we need to use the b_state flag of
  1932. * that buffer_head.
  1933. */
  1934. if (mpd->b_size == 0)
  1935. mpd->b_state =
  1936. bh->b_state & BH_FLAGS;
  1937. }
  1938. logical++;
  1939. } while ((bh = bh->b_this_page) != head);
  1940. if (nr_to_write > 0) {
  1941. nr_to_write--;
  1942. if (nr_to_write == 0 &&
  1943. wbc->sync_mode == WB_SYNC_NONE)
  1944. /*
  1945. * We stop writing back only if we are
  1946. * not doing integrity sync. In case of
  1947. * integrity sync we have to keep going
  1948. * because someone may be concurrently
  1949. * dirtying pages, and we might have
  1950. * synced a lot of newly appeared dirty
  1951. * pages, but have not synced all of the
  1952. * old dirty pages.
  1953. */
  1954. goto out;
  1955. }
  1956. }
  1957. pagevec_release(&pvec);
  1958. cond_resched();
  1959. }
  1960. return 0;
  1961. ret_extent_tail:
  1962. ret = MPAGE_DA_EXTENT_TAIL;
  1963. out:
  1964. pagevec_release(&pvec);
  1965. cond_resched();
  1966. return ret;
  1967. }
  1968. static int ext4_da_writepages(struct address_space *mapping,
  1969. struct writeback_control *wbc)
  1970. {
  1971. pgoff_t index;
  1972. int range_whole = 0;
  1973. handle_t *handle = NULL;
  1974. struct mpage_da_data mpd;
  1975. struct inode *inode = mapping->host;
  1976. int pages_written = 0;
  1977. unsigned int max_pages;
  1978. int range_cyclic, cycled = 1, io_done = 0;
  1979. int needed_blocks, ret = 0;
  1980. long desired_nr_to_write, nr_to_writebump = 0;
  1981. loff_t range_start = wbc->range_start;
  1982. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  1983. pgoff_t done_index = 0;
  1984. pgoff_t end;
  1985. struct blk_plug plug;
  1986. trace_ext4_da_writepages(inode, wbc);
  1987. /*
  1988. * No pages to write? This is mainly a kludge to avoid starting
  1989. * a transaction for special inodes like journal inode on last iput()
  1990. * because that could violate lock ordering on umount
  1991. */
  1992. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  1993. return 0;
  1994. /*
  1995. * If the filesystem has aborted, it is read-only, so return
  1996. * right away instead of dumping stack traces later on that
  1997. * will obscure the real source of the problem. We test
  1998. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  1999. * the latter could be true if the filesystem is mounted
  2000. * read-only, and in that case, ext4_da_writepages should
  2001. * *never* be called, so if that ever happens, we would want
  2002. * the stack trace.
  2003. */
  2004. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
  2005. return -EROFS;
  2006. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2007. range_whole = 1;
  2008. range_cyclic = wbc->range_cyclic;
  2009. if (wbc->range_cyclic) {
  2010. index = mapping->writeback_index;
  2011. if (index)
  2012. cycled = 0;
  2013. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2014. wbc->range_end = LLONG_MAX;
  2015. wbc->range_cyclic = 0;
  2016. end = -1;
  2017. } else {
  2018. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2019. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2020. }
  2021. /*
  2022. * This works around two forms of stupidity. The first is in
  2023. * the writeback code, which caps the maximum number of pages
  2024. * written to be 1024 pages. This is wrong on multiple
  2025. * levels; different architectues have a different page size,
  2026. * which changes the maximum amount of data which gets
  2027. * written. Secondly, 4 megabytes is way too small. XFS
  2028. * forces this value to be 16 megabytes by multiplying
  2029. * nr_to_write parameter by four, and then relies on its
  2030. * allocator to allocate larger extents to make them
  2031. * contiguous. Unfortunately this brings us to the second
  2032. * stupidity, which is that ext4's mballoc code only allocates
  2033. * at most 2048 blocks. So we force contiguous writes up to
  2034. * the number of dirty blocks in the inode, or
  2035. * sbi->max_writeback_mb_bump whichever is smaller.
  2036. */
  2037. max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
  2038. if (!range_cyclic && range_whole) {
  2039. if (wbc->nr_to_write == LONG_MAX)
  2040. desired_nr_to_write = wbc->nr_to_write;
  2041. else
  2042. desired_nr_to_write = wbc->nr_to_write * 8;
  2043. } else
  2044. desired_nr_to_write = ext4_num_dirty_pages(inode, index,
  2045. max_pages);
  2046. if (desired_nr_to_write > max_pages)
  2047. desired_nr_to_write = max_pages;
  2048. if (wbc->nr_to_write < desired_nr_to_write) {
  2049. nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
  2050. wbc->nr_to_write = desired_nr_to_write;
  2051. }
  2052. retry:
  2053. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2054. tag_pages_for_writeback(mapping, index, end);
  2055. blk_start_plug(&plug);
  2056. while (!ret && wbc->nr_to_write > 0) {
  2057. /*
  2058. * we insert one extent at a time. So we need
  2059. * credit needed for single extent allocation.
  2060. * journalled mode is currently not supported
  2061. * by delalloc
  2062. */
  2063. BUG_ON(ext4_should_journal_data(inode));
  2064. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2065. /* start a new transaction*/
  2066. handle = ext4_journal_start(inode, needed_blocks);
  2067. if (IS_ERR(handle)) {
  2068. ret = PTR_ERR(handle);
  2069. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2070. "%ld pages, ino %lu; err %d", __func__,
  2071. wbc->nr_to_write, inode->i_ino, ret);
  2072. blk_finish_plug(&plug);
  2073. goto out_writepages;
  2074. }
  2075. /*
  2076. * Now call write_cache_pages_da() to find the next
  2077. * contiguous region of logical blocks that need
  2078. * blocks to be allocated by ext4 and submit them.
  2079. */
  2080. ret = write_cache_pages_da(handle, mapping,
  2081. wbc, &mpd, &done_index);
  2082. /*
  2083. * If we have a contiguous extent of pages and we
  2084. * haven't done the I/O yet, map the blocks and submit
  2085. * them for I/O.
  2086. */
  2087. if (!mpd.io_done && mpd.next_page != mpd.first_page) {
  2088. mpage_da_map_and_submit(&mpd);
  2089. ret = MPAGE_DA_EXTENT_TAIL;
  2090. }
  2091. trace_ext4_da_write_pages(inode, &mpd);
  2092. wbc->nr_to_write -= mpd.pages_written;
  2093. ext4_journal_stop(handle);
  2094. if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
  2095. /* commit the transaction which would
  2096. * free blocks released in the transaction
  2097. * and try again
  2098. */
  2099. jbd2_journal_force_commit_nested(sbi->s_journal);
  2100. ret = 0;
  2101. } else if (ret == MPAGE_DA_EXTENT_TAIL) {
  2102. /*
  2103. * Got one extent now try with rest of the pages.
  2104. * If mpd.retval is set -EIO, journal is aborted.
  2105. * So we don't need to write any more.
  2106. */
  2107. pages_written += mpd.pages_written;
  2108. ret = mpd.retval;
  2109. io_done = 1;
  2110. } else if (wbc->nr_to_write)
  2111. /*
  2112. * There is no more writeout needed
  2113. * or we requested for a noblocking writeout
  2114. * and we found the device congested
  2115. */
  2116. break;
  2117. }
  2118. blk_finish_plug(&plug);
  2119. if (!io_done && !cycled) {
  2120. cycled = 1;
  2121. index = 0;
  2122. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2123. wbc->range_end = mapping->writeback_index - 1;
  2124. goto retry;
  2125. }
  2126. /* Update index */
  2127. wbc->range_cyclic = range_cyclic;
  2128. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2129. /*
  2130. * set the writeback_index so that range_cyclic
  2131. * mode will write it back later
  2132. */
  2133. mapping->writeback_index = done_index;
  2134. out_writepages:
  2135. wbc->nr_to_write -= nr_to_writebump;
  2136. wbc->range_start = range_start;
  2137. trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
  2138. return ret;
  2139. }
  2140. static int ext4_nonda_switch(struct super_block *sb)
  2141. {
  2142. s64 free_blocks, dirty_blocks;
  2143. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2144. /*
  2145. * switch to non delalloc mode if we are running low
  2146. * on free block. The free block accounting via percpu
  2147. * counters can get slightly wrong with percpu_counter_batch getting
  2148. * accumulated on each CPU without updating global counters
  2149. * Delalloc need an accurate free block accounting. So switch
  2150. * to non delalloc when we are near to error range.
  2151. */
  2152. free_blocks = EXT4_C2B(sbi,
  2153. percpu_counter_read_positive(&sbi->s_freeclusters_counter));
  2154. dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
  2155. /*
  2156. * Start pushing delalloc when 1/2 of free blocks are dirty.
  2157. */
  2158. if (dirty_blocks && (free_blocks < 2 * dirty_blocks) &&
  2159. !writeback_in_progress(sb->s_bdi) &&
  2160. down_read_trylock(&sb->s_umount)) {
  2161. writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
  2162. up_read(&sb->s_umount);
  2163. }
  2164. if (2 * free_blocks < 3 * dirty_blocks ||
  2165. free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
  2166. /*
  2167. * free block count is less than 150% of dirty blocks
  2168. * or free blocks is less than watermark
  2169. */
  2170. return 1;
  2171. }
  2172. return 0;
  2173. }
  2174. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2175. loff_t pos, unsigned len, unsigned flags,
  2176. struct page **pagep, void **fsdata)
  2177. {
  2178. int ret, retries = 0;
  2179. struct page *page;
  2180. pgoff_t index;
  2181. struct inode *inode = mapping->host;
  2182. handle_t *handle;
  2183. index = pos >> PAGE_CACHE_SHIFT;
  2184. if (ext4_nonda_switch(inode->i_sb)) {
  2185. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2186. return ext4_write_begin(file, mapping, pos,
  2187. len, flags, pagep, fsdata);
  2188. }
  2189. *fsdata = (void *)0;
  2190. trace_ext4_da_write_begin(inode, pos, len, flags);
  2191. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2192. ret = ext4_da_write_inline_data_begin(mapping, inode,
  2193. pos, len, flags,
  2194. pagep, fsdata);
  2195. if (ret < 0)
  2196. goto out;
  2197. if (ret == 1) {
  2198. ret = 0;
  2199. goto out;
  2200. }
  2201. }
  2202. retry:
  2203. /*
  2204. * With delayed allocation, we don't log the i_disksize update
  2205. * if there is delayed block allocation. But we still need
  2206. * to journalling the i_disksize update if writes to the end
  2207. * of file which has an already mapped buffer.
  2208. */
  2209. handle = ext4_journal_start(inode, 1);
  2210. if (IS_ERR(handle)) {
  2211. ret = PTR_ERR(handle);
  2212. goto out;
  2213. }
  2214. /* We cannot recurse into the filesystem as the transaction is already
  2215. * started */
  2216. flags |= AOP_FLAG_NOFS;
  2217. page = grab_cache_page_write_begin(mapping, index, flags);
  2218. if (!page) {
  2219. ext4_journal_stop(handle);
  2220. ret = -ENOMEM;
  2221. goto out;
  2222. }
  2223. *pagep = page;
  2224. ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
  2225. if (ret < 0) {
  2226. unlock_page(page);
  2227. ext4_journal_stop(handle);
  2228. page_cache_release(page);
  2229. /*
  2230. * block_write_begin may have instantiated a few blocks
  2231. * outside i_size. Trim these off again. Don't need
  2232. * i_size_read because we hold i_mutex.
  2233. */
  2234. if (pos + len > inode->i_size)
  2235. ext4_truncate_failed_write(inode);
  2236. }
  2237. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2238. goto retry;
  2239. out:
  2240. return ret;
  2241. }
  2242. /*
  2243. * Check if we should update i_disksize
  2244. * when write to the end of file but not require block allocation
  2245. */
  2246. static int ext4_da_should_update_i_disksize(struct page *page,
  2247. unsigned long offset)
  2248. {
  2249. struct buffer_head *bh;
  2250. struct inode *inode = page->mapping->host;
  2251. unsigned int idx;
  2252. int i;
  2253. bh = page_buffers(page);
  2254. idx = offset >> inode->i_blkbits;
  2255. for (i = 0; i < idx; i++)
  2256. bh = bh->b_this_page;
  2257. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2258. return 0;
  2259. return 1;
  2260. }
  2261. static int ext4_da_write_end(struct file *file,
  2262. struct address_space *mapping,
  2263. loff_t pos, unsigned len, unsigned copied,
  2264. struct page *page, void *fsdata)
  2265. {
  2266. struct inode *inode = mapping->host;
  2267. int ret = 0, ret2;
  2268. handle_t *handle = ext4_journal_current_handle();
  2269. loff_t new_i_size;
  2270. unsigned long start, end;
  2271. int write_mode = (int)(unsigned long)fsdata;
  2272. if (write_mode == FALL_BACK_TO_NONDELALLOC) {
  2273. switch (ext4_inode_journal_mode(inode)) {
  2274. case EXT4_INODE_ORDERED_DATA_MODE:
  2275. return ext4_ordered_write_end(file, mapping, pos,
  2276. len, copied, page, fsdata);
  2277. case EXT4_INODE_WRITEBACK_DATA_MODE:
  2278. return ext4_writeback_write_end(file, mapping, pos,
  2279. len, copied, page, fsdata);
  2280. default:
  2281. BUG();
  2282. }
  2283. }
  2284. trace_ext4_da_write_end(inode, pos, len, copied);
  2285. start = pos & (PAGE_CACHE_SIZE - 1);
  2286. end = start + copied - 1;
  2287. /*
  2288. * generic_write_end() will run mark_inode_dirty() if i_size
  2289. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2290. * into that.
  2291. */
  2292. new_i_size = pos + copied;
  2293. if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
  2294. if (ext4_has_inline_data(inode) ||
  2295. ext4_da_should_update_i_disksize(page, end)) {
  2296. down_write(&EXT4_I(inode)->i_data_sem);
  2297. if (new_i_size > EXT4_I(inode)->i_disksize)
  2298. EXT4_I(inode)->i_disksize = new_i_size;
  2299. up_write(&EXT4_I(inode)->i_data_sem);
  2300. /* We need to mark inode dirty even if
  2301. * new_i_size is less that inode->i_size
  2302. * bu greater than i_disksize.(hint delalloc)
  2303. */
  2304. ext4_mark_inode_dirty(handle, inode);
  2305. }
  2306. }
  2307. if (write_mode != CONVERT_INLINE_DATA &&
  2308. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
  2309. ext4_has_inline_data(inode))
  2310. ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
  2311. page);
  2312. else
  2313. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2314. page, fsdata);
  2315. copied = ret2;
  2316. if (ret2 < 0)
  2317. ret = ret2;
  2318. ret2 = ext4_journal_stop(handle);
  2319. if (!ret)
  2320. ret = ret2;
  2321. return ret ? ret : copied;
  2322. }
  2323. static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
  2324. {
  2325. /*
  2326. * Drop reserved blocks
  2327. */
  2328. BUG_ON(!PageLocked(page));
  2329. if (!page_has_buffers(page))
  2330. goto out;
  2331. ext4_da_page_release_reservation(page, offset);
  2332. out:
  2333. ext4_invalidatepage(page, offset);
  2334. return;
  2335. }
  2336. /*
  2337. * Force all delayed allocation blocks to be allocated for a given inode.
  2338. */
  2339. int ext4_alloc_da_blocks(struct inode *inode)
  2340. {
  2341. trace_ext4_alloc_da_blocks(inode);
  2342. if (!EXT4_I(inode)->i_reserved_data_blocks &&
  2343. !EXT4_I(inode)->i_reserved_meta_blocks)
  2344. return 0;
  2345. /*
  2346. * We do something simple for now. The filemap_flush() will
  2347. * also start triggering a write of the data blocks, which is
  2348. * not strictly speaking necessary (and for users of
  2349. * laptop_mode, not even desirable). However, to do otherwise
  2350. * would require replicating code paths in:
  2351. *
  2352. * ext4_da_writepages() ->
  2353. * write_cache_pages() ---> (via passed in callback function)
  2354. * __mpage_da_writepage() -->
  2355. * mpage_add_bh_to_extent()
  2356. * mpage_da_map_blocks()
  2357. *
  2358. * The problem is that write_cache_pages(), located in
  2359. * mm/page-writeback.c, marks pages clean in preparation for
  2360. * doing I/O, which is not desirable if we're not planning on
  2361. * doing I/O at all.
  2362. *
  2363. * We could call write_cache_pages(), and then redirty all of
  2364. * the pages by calling redirty_page_for_writepage() but that
  2365. * would be ugly in the extreme. So instead we would need to
  2366. * replicate parts of the code in the above functions,
  2367. * simplifying them because we wouldn't actually intend to
  2368. * write out the pages, but rather only collect contiguous
  2369. * logical block extents, call the multi-block allocator, and
  2370. * then update the buffer heads with the block allocations.
  2371. *
  2372. * For now, though, we'll cheat by calling filemap_flush(),
  2373. * which will map the blocks, and start the I/O, but not
  2374. * actually wait for the I/O to complete.
  2375. */
  2376. return filemap_flush(inode->i_mapping);
  2377. }
  2378. /*
  2379. * bmap() is special. It gets used by applications such as lilo and by
  2380. * the swapper to find the on-disk block of a specific piece of data.
  2381. *
  2382. * Naturally, this is dangerous if the block concerned is still in the
  2383. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2384. * filesystem and enables swap, then they may get a nasty shock when the
  2385. * data getting swapped to that swapfile suddenly gets overwritten by
  2386. * the original zero's written out previously to the journal and
  2387. * awaiting writeback in the kernel's buffer cache.
  2388. *
  2389. * So, if we see any bmap calls here on a modified, data-journaled file,
  2390. * take extra steps to flush any blocks which might be in the cache.
  2391. */
  2392. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2393. {
  2394. struct inode *inode = mapping->host;
  2395. journal_t *journal;
  2396. int err;
  2397. /*
  2398. * We can get here for an inline file via the FIBMAP ioctl
  2399. */
  2400. if (ext4_has_inline_data(inode))
  2401. return 0;
  2402. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2403. test_opt(inode->i_sb, DELALLOC)) {
  2404. /*
  2405. * With delalloc we want to sync the file
  2406. * so that we can make sure we allocate
  2407. * blocks for file
  2408. */
  2409. filemap_write_and_wait(mapping);
  2410. }
  2411. if (EXT4_JOURNAL(inode) &&
  2412. ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
  2413. /*
  2414. * This is a REALLY heavyweight approach, but the use of
  2415. * bmap on dirty files is expected to be extremely rare:
  2416. * only if we run lilo or swapon on a freshly made file
  2417. * do we expect this to happen.
  2418. *
  2419. * (bmap requires CAP_SYS_RAWIO so this does not
  2420. * represent an unprivileged user DOS attack --- we'd be
  2421. * in trouble if mortal users could trigger this path at
  2422. * will.)
  2423. *
  2424. * NB. EXT4_STATE_JDATA is not set on files other than
  2425. * regular files. If somebody wants to bmap a directory
  2426. * or symlink and gets confused because the buffer
  2427. * hasn't yet been flushed to disk, they deserve
  2428. * everything they get.
  2429. */
  2430. ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
  2431. journal = EXT4_JOURNAL(inode);
  2432. jbd2_journal_lock_updates(journal);
  2433. err = jbd2_journal_flush(journal);
  2434. jbd2_journal_unlock_updates(journal);
  2435. if (err)
  2436. return 0;
  2437. }
  2438. return generic_block_bmap(mapping, block, ext4_get_block);
  2439. }
  2440. static int ext4_readpage(struct file *file, struct page *page)
  2441. {
  2442. int ret = -EAGAIN;
  2443. struct inode *inode = page->mapping->host;
  2444. trace_ext4_readpage(page);
  2445. if (ext4_has_inline_data(inode))
  2446. ret = ext4_readpage_inline(inode, page);
  2447. if (ret == -EAGAIN)
  2448. return mpage_readpage(page, ext4_get_block);
  2449. return ret;
  2450. }
  2451. static int
  2452. ext4_readpages(struct file *file, struct address_space *mapping,
  2453. struct list_head *pages, unsigned nr_pages)
  2454. {
  2455. struct inode *inode = mapping->host;
  2456. /* If the file has inline data, no need to do readpages. */
  2457. if (ext4_has_inline_data(inode))
  2458. return 0;
  2459. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2460. }
  2461. static void ext4_invalidatepage(struct page *page, unsigned long offset)
  2462. {
  2463. trace_ext4_invalidatepage(page, offset);
  2464. /* No journalling happens on data buffers when this function is used */
  2465. WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
  2466. block_invalidatepage(page, offset);
  2467. }
  2468. static int __ext4_journalled_invalidatepage(struct page *page,
  2469. unsigned long offset)
  2470. {
  2471. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2472. trace_ext4_journalled_invalidatepage(page, offset);
  2473. /*
  2474. * If it's a full truncate we just forget about the pending dirtying
  2475. */
  2476. if (offset == 0)
  2477. ClearPageChecked(page);
  2478. return jbd2_journal_invalidatepage(journal, page, offset);
  2479. }
  2480. /* Wrapper for aops... */
  2481. static void ext4_journalled_invalidatepage(struct page *page,
  2482. unsigned long offset)
  2483. {
  2484. WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
  2485. }
  2486. static int ext4_releasepage(struct page *page, gfp_t wait)
  2487. {
  2488. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2489. trace_ext4_releasepage(page);
  2490. WARN_ON(PageChecked(page));
  2491. if (!page_has_buffers(page))
  2492. return 0;
  2493. if (journal)
  2494. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2495. else
  2496. return try_to_free_buffers(page);
  2497. }
  2498. /*
  2499. * ext4_get_block used when preparing for a DIO write or buffer write.
  2500. * We allocate an uinitialized extent if blocks haven't been allocated.
  2501. * The extent will be converted to initialized after the IO is complete.
  2502. */
  2503. int ext4_get_block_write(struct inode *inode, sector_t iblock,
  2504. struct buffer_head *bh_result, int create)
  2505. {
  2506. ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
  2507. inode->i_ino, create);
  2508. return _ext4_get_block(inode, iblock, bh_result,
  2509. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  2510. }
  2511. static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
  2512. struct buffer_head *bh_result, int create)
  2513. {
  2514. ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
  2515. inode->i_ino, create);
  2516. return _ext4_get_block(inode, iblock, bh_result,
  2517. EXT4_GET_BLOCKS_NO_LOCK);
  2518. }
  2519. static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
  2520. ssize_t size, void *private, int ret,
  2521. bool is_async)
  2522. {
  2523. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  2524. ext4_io_end_t *io_end = iocb->private;
  2525. /* if not async direct IO or dio with 0 bytes write, just return */
  2526. if (!io_end || !size)
  2527. goto out;
  2528. ext_debug("ext4_end_io_dio(): io_end 0x%p "
  2529. "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
  2530. iocb->private, io_end->inode->i_ino, iocb, offset,
  2531. size);
  2532. iocb->private = NULL;
  2533. /* if not aio dio with unwritten extents, just free io and return */
  2534. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  2535. ext4_free_io_end(io_end);
  2536. out:
  2537. if (is_async)
  2538. aio_complete(iocb, ret, 0);
  2539. inode_dio_done(inode);
  2540. return;
  2541. }
  2542. io_end->offset = offset;
  2543. io_end->size = size;
  2544. if (is_async) {
  2545. io_end->iocb = iocb;
  2546. io_end->result = ret;
  2547. }
  2548. ext4_add_complete_io(io_end);
  2549. }
  2550. /*
  2551. * For ext4 extent files, ext4 will do direct-io write to holes,
  2552. * preallocated extents, and those write extend the file, no need to
  2553. * fall back to buffered IO.
  2554. *
  2555. * For holes, we fallocate those blocks, mark them as uninitialized
  2556. * If those blocks were preallocated, we mark sure they are split, but
  2557. * still keep the range to write as uninitialized.
  2558. *
  2559. * The unwritten extents will be converted to written when DIO is completed.
  2560. * For async direct IO, since the IO may still pending when return, we
  2561. * set up an end_io call back function, which will do the conversion
  2562. * when async direct IO completed.
  2563. *
  2564. * If the O_DIRECT write will extend the file then add this inode to the
  2565. * orphan list. So recovery will truncate it back to the original size
  2566. * if the machine crashes during the write.
  2567. *
  2568. */
  2569. static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
  2570. const struct iovec *iov, loff_t offset,
  2571. unsigned long nr_segs)
  2572. {
  2573. struct file *file = iocb->ki_filp;
  2574. struct inode *inode = file->f_mapping->host;
  2575. ssize_t ret;
  2576. size_t count = iov_length(iov, nr_segs);
  2577. int overwrite = 0;
  2578. get_block_t *get_block_func = NULL;
  2579. int dio_flags = 0;
  2580. loff_t final_size = offset + count;
  2581. /* Use the old path for reads and writes beyond i_size. */
  2582. if (rw != WRITE || final_size > inode->i_size)
  2583. return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2584. BUG_ON(iocb->private == NULL);
  2585. /* If we do a overwrite dio, i_mutex locking can be released */
  2586. overwrite = *((int *)iocb->private);
  2587. if (overwrite) {
  2588. atomic_inc(&inode->i_dio_count);
  2589. down_read(&EXT4_I(inode)->i_data_sem);
  2590. mutex_unlock(&inode->i_mutex);
  2591. }
  2592. /*
  2593. * We could direct write to holes and fallocate.
  2594. *
  2595. * Allocated blocks to fill the hole are marked as
  2596. * uninitialized to prevent parallel buffered read to expose
  2597. * the stale data before DIO complete the data IO.
  2598. *
  2599. * As to previously fallocated extents, ext4 get_block will
  2600. * just simply mark the buffer mapped but still keep the
  2601. * extents uninitialized.
  2602. *
  2603. * For non AIO case, we will convert those unwritten extents
  2604. * to written after return back from blockdev_direct_IO.
  2605. *
  2606. * For async DIO, the conversion needs to be deferred when the
  2607. * IO is completed. The ext4 end_io callback function will be
  2608. * called to take care of the conversion work. Here for async
  2609. * case, we allocate an io_end structure to hook to the iocb.
  2610. */
  2611. iocb->private = NULL;
  2612. ext4_inode_aio_set(inode, NULL);
  2613. if (!is_sync_kiocb(iocb)) {
  2614. ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS);
  2615. if (!io_end) {
  2616. ret = -ENOMEM;
  2617. goto retake_lock;
  2618. }
  2619. io_end->flag |= EXT4_IO_END_DIRECT;
  2620. iocb->private = io_end;
  2621. /*
  2622. * we save the io structure for current async direct
  2623. * IO, so that later ext4_map_blocks() could flag the
  2624. * io structure whether there is a unwritten extents
  2625. * needs to be converted when IO is completed.
  2626. */
  2627. ext4_inode_aio_set(inode, io_end);
  2628. }
  2629. if (overwrite) {
  2630. get_block_func = ext4_get_block_write_nolock;
  2631. } else {
  2632. get_block_func = ext4_get_block_write;
  2633. dio_flags = DIO_LOCKING;
  2634. }
  2635. ret = __blockdev_direct_IO(rw, iocb, inode,
  2636. inode->i_sb->s_bdev, iov,
  2637. offset, nr_segs,
  2638. get_block_func,
  2639. ext4_end_io_dio,
  2640. NULL,
  2641. dio_flags);
  2642. if (iocb->private)
  2643. ext4_inode_aio_set(inode, NULL);
  2644. /*
  2645. * The io_end structure takes a reference to the inode, that
  2646. * structure needs to be destroyed and the reference to the
  2647. * inode need to be dropped, when IO is complete, even with 0
  2648. * byte write, or failed.
  2649. *
  2650. * In the successful AIO DIO case, the io_end structure will
  2651. * be destroyed and the reference to the inode will be dropped
  2652. * after the end_io call back function is called.
  2653. *
  2654. * In the case there is 0 byte write, or error case, since VFS
  2655. * direct IO won't invoke the end_io call back function, we
  2656. * need to free the end_io structure here.
  2657. */
  2658. if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
  2659. ext4_free_io_end(iocb->private);
  2660. iocb->private = NULL;
  2661. } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
  2662. EXT4_STATE_DIO_UNWRITTEN)) {
  2663. int err;
  2664. /*
  2665. * for non AIO case, since the IO is already
  2666. * completed, we could do the conversion right here
  2667. */
  2668. err = ext4_convert_unwritten_extents(inode,
  2669. offset, ret);
  2670. if (err < 0)
  2671. ret = err;
  2672. ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  2673. }
  2674. retake_lock:
  2675. /* take i_mutex locking again if we do a ovewrite dio */
  2676. if (overwrite) {
  2677. inode_dio_done(inode);
  2678. up_read(&EXT4_I(inode)->i_data_sem);
  2679. mutex_lock(&inode->i_mutex);
  2680. }
  2681. return ret;
  2682. }
  2683. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  2684. const struct iovec *iov, loff_t offset,
  2685. unsigned long nr_segs)
  2686. {
  2687. struct file *file = iocb->ki_filp;
  2688. struct inode *inode = file->f_mapping->host;
  2689. ssize_t ret;
  2690. /*
  2691. * If we are doing data journalling we don't support O_DIRECT
  2692. */
  2693. if (ext4_should_journal_data(inode))
  2694. return 0;
  2695. /* Let buffer I/O handle the inline data case. */
  2696. if (ext4_has_inline_data(inode))
  2697. return 0;
  2698. trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
  2699. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  2700. ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
  2701. else
  2702. ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2703. trace_ext4_direct_IO_exit(inode, offset,
  2704. iov_length(iov, nr_segs), rw, ret);
  2705. return ret;
  2706. }
  2707. /*
  2708. * Pages can be marked dirty completely asynchronously from ext4's journalling
  2709. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  2710. * much here because ->set_page_dirty is called under VFS locks. The page is
  2711. * not necessarily locked.
  2712. *
  2713. * We cannot just dirty the page and leave attached buffers clean, because the
  2714. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  2715. * or jbddirty because all the journalling code will explode.
  2716. *
  2717. * So what we do is to mark the page "pending dirty" and next time writepage
  2718. * is called, propagate that into the buffers appropriately.
  2719. */
  2720. static int ext4_journalled_set_page_dirty(struct page *page)
  2721. {
  2722. SetPageChecked(page);
  2723. return __set_page_dirty_nobuffers(page);
  2724. }
  2725. static const struct address_space_operations ext4_ordered_aops = {
  2726. .readpage = ext4_readpage,
  2727. .readpages = ext4_readpages,
  2728. .writepage = ext4_writepage,
  2729. .write_begin = ext4_write_begin,
  2730. .write_end = ext4_ordered_write_end,
  2731. .bmap = ext4_bmap,
  2732. .invalidatepage = ext4_invalidatepage,
  2733. .releasepage = ext4_releasepage,
  2734. .direct_IO = ext4_direct_IO,
  2735. .migratepage = buffer_migrate_page,
  2736. .is_partially_uptodate = block_is_partially_uptodate,
  2737. .error_remove_page = generic_error_remove_page,
  2738. };
  2739. static const struct address_space_operations ext4_writeback_aops = {
  2740. .readpage = ext4_readpage,
  2741. .readpages = ext4_readpages,
  2742. .writepage = ext4_writepage,
  2743. .write_begin = ext4_write_begin,
  2744. .write_end = ext4_writeback_write_end,
  2745. .bmap = ext4_bmap,
  2746. .invalidatepage = ext4_invalidatepage,
  2747. .releasepage = ext4_releasepage,
  2748. .direct_IO = ext4_direct_IO,
  2749. .migratepage = buffer_migrate_page,
  2750. .is_partially_uptodate = block_is_partially_uptodate,
  2751. .error_remove_page = generic_error_remove_page,
  2752. };
  2753. static const struct address_space_operations ext4_journalled_aops = {
  2754. .readpage = ext4_readpage,
  2755. .readpages = ext4_readpages,
  2756. .writepage = ext4_writepage,
  2757. .write_begin = ext4_write_begin,
  2758. .write_end = ext4_journalled_write_end,
  2759. .set_page_dirty = ext4_journalled_set_page_dirty,
  2760. .bmap = ext4_bmap,
  2761. .invalidatepage = ext4_journalled_invalidatepage,
  2762. .releasepage = ext4_releasepage,
  2763. .direct_IO = ext4_direct_IO,
  2764. .is_partially_uptodate = block_is_partially_uptodate,
  2765. .error_remove_page = generic_error_remove_page,
  2766. };
  2767. static const struct address_space_operations ext4_da_aops = {
  2768. .readpage = ext4_readpage,
  2769. .readpages = ext4_readpages,
  2770. .writepage = ext4_writepage,
  2771. .writepages = ext4_da_writepages,
  2772. .write_begin = ext4_da_write_begin,
  2773. .write_end = ext4_da_write_end,
  2774. .bmap = ext4_bmap,
  2775. .invalidatepage = ext4_da_invalidatepage,
  2776. .releasepage = ext4_releasepage,
  2777. .direct_IO = ext4_direct_IO,
  2778. .migratepage = buffer_migrate_page,
  2779. .is_partially_uptodate = block_is_partially_uptodate,
  2780. .error_remove_page = generic_error_remove_page,
  2781. };
  2782. void ext4_set_aops(struct inode *inode)
  2783. {
  2784. switch (ext4_inode_journal_mode(inode)) {
  2785. case EXT4_INODE_ORDERED_DATA_MODE:
  2786. if (test_opt(inode->i_sb, DELALLOC))
  2787. inode->i_mapping->a_ops = &ext4_da_aops;
  2788. else
  2789. inode->i_mapping->a_ops = &ext4_ordered_aops;
  2790. break;
  2791. case EXT4_INODE_WRITEBACK_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_writeback_aops;
  2796. break;
  2797. case EXT4_INODE_JOURNAL_DATA_MODE:
  2798. inode->i_mapping->a_ops = &ext4_journalled_aops;
  2799. break;
  2800. default:
  2801. BUG();
  2802. }
  2803. }
  2804. /*
  2805. * ext4_discard_partial_page_buffers()
  2806. * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
  2807. * This function finds and locks the page containing the offset
  2808. * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
  2809. * Calling functions that already have the page locked should call
  2810. * ext4_discard_partial_page_buffers_no_lock directly.
  2811. */
  2812. int ext4_discard_partial_page_buffers(handle_t *handle,
  2813. struct address_space *mapping, loff_t from,
  2814. loff_t length, int flags)
  2815. {
  2816. struct inode *inode = mapping->host;
  2817. struct page *page;
  2818. int err = 0;
  2819. page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
  2820. mapping_gfp_mask(mapping) & ~__GFP_FS);
  2821. if (!page)
  2822. return -ENOMEM;
  2823. err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
  2824. from, length, flags);
  2825. unlock_page(page);
  2826. page_cache_release(page);
  2827. return err;
  2828. }
  2829. /*
  2830. * ext4_discard_partial_page_buffers_no_lock()
  2831. * Zeros a page range of length 'length' starting from offset 'from'.
  2832. * Buffer heads that correspond to the block aligned regions of the
  2833. * zeroed range will be unmapped. Unblock aligned regions
  2834. * will have the corresponding buffer head mapped if needed so that
  2835. * that region of the page can be updated with the partial zero out.
  2836. *
  2837. * This function assumes that the page has already been locked. The
  2838. * The range to be discarded must be contained with in the given page.
  2839. * If the specified range exceeds the end of the page it will be shortened
  2840. * to the end of the page that corresponds to 'from'. This function is
  2841. * appropriate for updating a page and it buffer heads to be unmapped and
  2842. * zeroed for blocks that have been either released, or are going to be
  2843. * released.
  2844. *
  2845. * handle: The journal handle
  2846. * inode: The files inode
  2847. * page: A locked page that contains the offset "from"
  2848. * from: The starting byte offset (from the beginning of the file)
  2849. * to begin discarding
  2850. * len: The length of bytes to discard
  2851. * flags: Optional flags that may be used:
  2852. *
  2853. * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
  2854. * Only zero the regions of the page whose buffer heads
  2855. * have already been unmapped. This flag is appropriate
  2856. * for updating the contents of a page whose blocks may
  2857. * have already been released, and we only want to zero
  2858. * out the regions that correspond to those released blocks.
  2859. *
  2860. * Returns zero on success or negative on failure.
  2861. */
  2862. static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
  2863. struct inode *inode, struct page *page, loff_t from,
  2864. loff_t length, int flags)
  2865. {
  2866. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  2867. unsigned int offset = from & (PAGE_CACHE_SIZE-1);
  2868. unsigned int blocksize, max, pos;
  2869. ext4_lblk_t iblock;
  2870. struct buffer_head *bh;
  2871. int err = 0;
  2872. blocksize = inode->i_sb->s_blocksize;
  2873. max = PAGE_CACHE_SIZE - offset;
  2874. if (index != page->index)
  2875. return -EINVAL;
  2876. /*
  2877. * correct length if it does not fall between
  2878. * 'from' and the end of the page
  2879. */
  2880. if (length > max || length < 0)
  2881. length = max;
  2882. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  2883. if (!page_has_buffers(page))
  2884. create_empty_buffers(page, blocksize, 0);
  2885. /* Find the buffer that contains "offset" */
  2886. bh = page_buffers(page);
  2887. pos = blocksize;
  2888. while (offset >= pos) {
  2889. bh = bh->b_this_page;
  2890. iblock++;
  2891. pos += blocksize;
  2892. }
  2893. pos = offset;
  2894. while (pos < offset + length) {
  2895. unsigned int end_of_block, range_to_discard;
  2896. err = 0;
  2897. /* The length of space left to zero and unmap */
  2898. range_to_discard = offset + length - pos;
  2899. /* The length of space until the end of the block */
  2900. end_of_block = blocksize - (pos & (blocksize-1));
  2901. /*
  2902. * Do not unmap or zero past end of block
  2903. * for this buffer head
  2904. */
  2905. if (range_to_discard > end_of_block)
  2906. range_to_discard = end_of_block;
  2907. /*
  2908. * Skip this buffer head if we are only zeroing unampped
  2909. * regions of the page
  2910. */
  2911. if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
  2912. buffer_mapped(bh))
  2913. goto next;
  2914. /* If the range is block aligned, unmap */
  2915. if (range_to_discard == blocksize) {
  2916. clear_buffer_dirty(bh);
  2917. bh->b_bdev = NULL;
  2918. clear_buffer_mapped(bh);
  2919. clear_buffer_req(bh);
  2920. clear_buffer_new(bh);
  2921. clear_buffer_delay(bh);
  2922. clear_buffer_unwritten(bh);
  2923. clear_buffer_uptodate(bh);
  2924. zero_user(page, pos, range_to_discard);
  2925. BUFFER_TRACE(bh, "Buffer discarded");
  2926. goto next;
  2927. }
  2928. /*
  2929. * If this block is not completely contained in the range
  2930. * to be discarded, then it is not going to be released. Because
  2931. * we need to keep this block, we need to make sure this part
  2932. * of the page is uptodate before we modify it by writeing
  2933. * partial zeros on it.
  2934. */
  2935. if (!buffer_mapped(bh)) {
  2936. /*
  2937. * Buffer head must be mapped before we can read
  2938. * from the block
  2939. */
  2940. BUFFER_TRACE(bh, "unmapped");
  2941. ext4_get_block(inode, iblock, bh, 0);
  2942. /* unmapped? It's a hole - nothing to do */
  2943. if (!buffer_mapped(bh)) {
  2944. BUFFER_TRACE(bh, "still unmapped");
  2945. goto next;
  2946. }
  2947. }
  2948. /* Ok, it's mapped. Make sure it's up-to-date */
  2949. if (PageUptodate(page))
  2950. set_buffer_uptodate(bh);
  2951. if (!buffer_uptodate(bh)) {
  2952. err = -EIO;
  2953. ll_rw_block(READ, 1, &bh);
  2954. wait_on_buffer(bh);
  2955. /* Uhhuh. Read error. Complain and punt.*/
  2956. if (!buffer_uptodate(bh))
  2957. goto next;
  2958. }
  2959. if (ext4_should_journal_data(inode)) {
  2960. BUFFER_TRACE(bh, "get write access");
  2961. err = ext4_journal_get_write_access(handle, bh);
  2962. if (err)
  2963. goto next;
  2964. }
  2965. zero_user(page, pos, range_to_discard);
  2966. err = 0;
  2967. if (ext4_should_journal_data(inode)) {
  2968. err = ext4_handle_dirty_metadata(handle, inode, bh);
  2969. } else
  2970. mark_buffer_dirty(bh);
  2971. BUFFER_TRACE(bh, "Partial buffer zeroed");
  2972. next:
  2973. bh = bh->b_this_page;
  2974. iblock++;
  2975. pos += range_to_discard;
  2976. }
  2977. return err;
  2978. }
  2979. int ext4_can_truncate(struct inode *inode)
  2980. {
  2981. if (S_ISREG(inode->i_mode))
  2982. return 1;
  2983. if (S_ISDIR(inode->i_mode))
  2984. return 1;
  2985. if (S_ISLNK(inode->i_mode))
  2986. return !ext4_inode_is_fast_symlink(inode);
  2987. return 0;
  2988. }
  2989. /*
  2990. * ext4_punch_hole: punches a hole in a file by releaseing the blocks
  2991. * associated with the given offset and length
  2992. *
  2993. * @inode: File inode
  2994. * @offset: The offset where the hole will begin
  2995. * @len: The length of the hole
  2996. *
  2997. * Returns: 0 on success or negative on failure
  2998. */
  2999. int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
  3000. {
  3001. struct inode *inode = file->f_path.dentry->d_inode;
  3002. if (!S_ISREG(inode->i_mode))
  3003. return -EOPNOTSUPP;
  3004. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3005. return ext4_ind_punch_hole(file, offset, length);
  3006. if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) {
  3007. /* TODO: Add support for bigalloc file systems */
  3008. return -EOPNOTSUPP;
  3009. }
  3010. trace_ext4_punch_hole(inode, offset, length);
  3011. return ext4_ext_punch_hole(file, offset, length);
  3012. }
  3013. /*
  3014. * ext4_truncate()
  3015. *
  3016. * We block out ext4_get_block() block instantiations across the entire
  3017. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3018. * simultaneously on behalf of the same inode.
  3019. *
  3020. * As we work through the truncate and commit bits of it to the journal there
  3021. * is one core, guiding principle: the file's tree must always be consistent on
  3022. * disk. We must be able to restart the truncate after a crash.
  3023. *
  3024. * The file's tree may be transiently inconsistent in memory (although it
  3025. * probably isn't), but whenever we close off and commit a journal transaction,
  3026. * the contents of (the filesystem + the journal) must be consistent and
  3027. * restartable. It's pretty simple, really: bottom up, right to left (although
  3028. * left-to-right works OK too).
  3029. *
  3030. * Note that at recovery time, journal replay occurs *before* the restart of
  3031. * truncate against the orphan inode list.
  3032. *
  3033. * The committed inode has the new, desired i_size (which is the same as
  3034. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3035. * that this inode's truncate did not complete and it will again call
  3036. * ext4_truncate() to have another go. So there will be instantiated blocks
  3037. * to the right of the truncation point in a crashed ext4 filesystem. But
  3038. * that's fine - as long as they are linked from the inode, the post-crash
  3039. * ext4_truncate() run will find them and release them.
  3040. */
  3041. void ext4_truncate(struct inode *inode)
  3042. {
  3043. trace_ext4_truncate_enter(inode);
  3044. if (!ext4_can_truncate(inode))
  3045. return;
  3046. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3047. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3048. ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  3049. if (ext4_has_inline_data(inode)) {
  3050. int has_inline = 1;
  3051. ext4_inline_data_truncate(inode, &has_inline);
  3052. if (has_inline)
  3053. return;
  3054. }
  3055. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3056. ext4_ext_truncate(inode);
  3057. else
  3058. ext4_ind_truncate(inode);
  3059. trace_ext4_truncate_exit(inode);
  3060. }
  3061. /*
  3062. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3063. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3064. * data in memory that is needed to recreate the on-disk version of this
  3065. * inode.
  3066. */
  3067. static int __ext4_get_inode_loc(struct inode *inode,
  3068. struct ext4_iloc *iloc, int in_mem)
  3069. {
  3070. struct ext4_group_desc *gdp;
  3071. struct buffer_head *bh;
  3072. struct super_block *sb = inode->i_sb;
  3073. ext4_fsblk_t block;
  3074. int inodes_per_block, inode_offset;
  3075. iloc->bh = NULL;
  3076. if (!ext4_valid_inum(sb, inode->i_ino))
  3077. return -EIO;
  3078. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3079. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  3080. if (!gdp)
  3081. return -EIO;
  3082. /*
  3083. * Figure out the offset within the block group inode table
  3084. */
  3085. inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  3086. inode_offset = ((inode->i_ino - 1) %
  3087. EXT4_INODES_PER_GROUP(sb));
  3088. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  3089. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  3090. bh = sb_getblk(sb, block);
  3091. if (unlikely(!bh))
  3092. return -ENOMEM;
  3093. if (!buffer_uptodate(bh)) {
  3094. lock_buffer(bh);
  3095. /*
  3096. * If the buffer has the write error flag, we have failed
  3097. * to write out another inode in the same block. In this
  3098. * case, we don't have to read the block because we may
  3099. * read the old inode data successfully.
  3100. */
  3101. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3102. set_buffer_uptodate(bh);
  3103. if (buffer_uptodate(bh)) {
  3104. /* someone brought it uptodate while we waited */
  3105. unlock_buffer(bh);
  3106. goto has_buffer;
  3107. }
  3108. /*
  3109. * If we have all information of the inode in memory and this
  3110. * is the only valid inode in the block, we need not read the
  3111. * block.
  3112. */
  3113. if (in_mem) {
  3114. struct buffer_head *bitmap_bh;
  3115. int i, start;
  3116. start = inode_offset & ~(inodes_per_block - 1);
  3117. /* Is the inode bitmap in cache? */
  3118. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  3119. if (unlikely(!bitmap_bh))
  3120. goto make_io;
  3121. /*
  3122. * If the inode bitmap isn't in cache then the
  3123. * optimisation may end up performing two reads instead
  3124. * of one, so skip it.
  3125. */
  3126. if (!buffer_uptodate(bitmap_bh)) {
  3127. brelse(bitmap_bh);
  3128. goto make_io;
  3129. }
  3130. for (i = start; i < start + inodes_per_block; i++) {
  3131. if (i == inode_offset)
  3132. continue;
  3133. if (ext4_test_bit(i, bitmap_bh->b_data))
  3134. break;
  3135. }
  3136. brelse(bitmap_bh);
  3137. if (i == start + inodes_per_block) {
  3138. /* all other inodes are free, so skip I/O */
  3139. memset(bh->b_data, 0, bh->b_size);
  3140. set_buffer_uptodate(bh);
  3141. unlock_buffer(bh);
  3142. goto has_buffer;
  3143. }
  3144. }
  3145. make_io:
  3146. /*
  3147. * If we need to do any I/O, try to pre-readahead extra
  3148. * blocks from the inode table.
  3149. */
  3150. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  3151. ext4_fsblk_t b, end, table;
  3152. unsigned num;
  3153. table = ext4_inode_table(sb, gdp);
  3154. /* s_inode_readahead_blks is always a power of 2 */
  3155. b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
  3156. if (table > b)
  3157. b = table;
  3158. end = b + EXT4_SB(sb)->s_inode_readahead_blks;
  3159. num = EXT4_INODES_PER_GROUP(sb);
  3160. if (ext4_has_group_desc_csum(sb))
  3161. num -= ext4_itable_unused_count(sb, gdp);
  3162. table += num / inodes_per_block;
  3163. if (end > table)
  3164. end = table;
  3165. while (b <= end)
  3166. sb_breadahead(sb, b++);
  3167. }
  3168. /*
  3169. * There are other valid inodes in the buffer, this inode
  3170. * has in-inode xattrs, or we don't have this inode in memory.
  3171. * Read the block from disk.
  3172. */
  3173. trace_ext4_load_inode(inode);
  3174. get_bh(bh);
  3175. bh->b_end_io = end_buffer_read_sync;
  3176. submit_bh(READ | REQ_META | REQ_PRIO, bh);
  3177. wait_on_buffer(bh);
  3178. if (!buffer_uptodate(bh)) {
  3179. EXT4_ERROR_INODE_BLOCK(inode, block,
  3180. "unable to read itable block");
  3181. brelse(bh);
  3182. return -EIO;
  3183. }
  3184. }
  3185. has_buffer:
  3186. iloc->bh = bh;
  3187. return 0;
  3188. }
  3189. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3190. {
  3191. /* We have all inode data except xattrs in memory here. */
  3192. return __ext4_get_inode_loc(inode, iloc,
  3193. !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
  3194. }
  3195. void ext4_set_inode_flags(struct inode *inode)
  3196. {
  3197. unsigned int flags = EXT4_I(inode)->i_flags;
  3198. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  3199. if (flags & EXT4_SYNC_FL)
  3200. inode->i_flags |= S_SYNC;
  3201. if (flags & EXT4_APPEND_FL)
  3202. inode->i_flags |= S_APPEND;
  3203. if (flags & EXT4_IMMUTABLE_FL)
  3204. inode->i_flags |= S_IMMUTABLE;
  3205. if (flags & EXT4_NOATIME_FL)
  3206. inode->i_flags |= S_NOATIME;
  3207. if (flags & EXT4_DIRSYNC_FL)
  3208. inode->i_flags |= S_DIRSYNC;
  3209. }
  3210. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3211. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3212. {
  3213. unsigned int vfs_fl;
  3214. unsigned long old_fl, new_fl;
  3215. do {
  3216. vfs_fl = ei->vfs_inode.i_flags;
  3217. old_fl = ei->i_flags;
  3218. new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3219. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
  3220. EXT4_DIRSYNC_FL);
  3221. if (vfs_fl & S_SYNC)
  3222. new_fl |= EXT4_SYNC_FL;
  3223. if (vfs_fl & S_APPEND)
  3224. new_fl |= EXT4_APPEND_FL;
  3225. if (vfs_fl & S_IMMUTABLE)
  3226. new_fl |= EXT4_IMMUTABLE_FL;
  3227. if (vfs_fl & S_NOATIME)
  3228. new_fl |= EXT4_NOATIME_FL;
  3229. if (vfs_fl & S_DIRSYNC)
  3230. new_fl |= EXT4_DIRSYNC_FL;
  3231. } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
  3232. }
  3233. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3234. struct ext4_inode_info *ei)
  3235. {
  3236. blkcnt_t i_blocks ;
  3237. struct inode *inode = &(ei->vfs_inode);
  3238. struct super_block *sb = inode->i_sb;
  3239. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3240. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  3241. /* we are using combined 48 bit field */
  3242. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3243. le32_to_cpu(raw_inode->i_blocks_lo);
  3244. if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
  3245. /* i_blocks represent file system block size */
  3246. return i_blocks << (inode->i_blkbits - 9);
  3247. } else {
  3248. return i_blocks;
  3249. }
  3250. } else {
  3251. return le32_to_cpu(raw_inode->i_blocks_lo);
  3252. }
  3253. }
  3254. static inline void ext4_iget_extra_inode(struct inode *inode,
  3255. struct ext4_inode *raw_inode,
  3256. struct ext4_inode_info *ei)
  3257. {
  3258. __le32 *magic = (void *)raw_inode +
  3259. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
  3260. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
  3261. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  3262. ext4_find_inline_data_nolock(inode);
  3263. } else
  3264. EXT4_I(inode)->i_inline_off = 0;
  3265. }
  3266. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3267. {
  3268. struct ext4_iloc iloc;
  3269. struct ext4_inode *raw_inode;
  3270. struct ext4_inode_info *ei;
  3271. struct inode *inode;
  3272. journal_t *journal = EXT4_SB(sb)->s_journal;
  3273. long ret;
  3274. int block;
  3275. uid_t i_uid;
  3276. gid_t i_gid;
  3277. inode = iget_locked(sb, ino);
  3278. if (!inode)
  3279. return ERR_PTR(-ENOMEM);
  3280. if (!(inode->i_state & I_NEW))
  3281. return inode;
  3282. ei = EXT4_I(inode);
  3283. iloc.bh = NULL;
  3284. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3285. if (ret < 0)
  3286. goto bad_inode;
  3287. raw_inode = ext4_raw_inode(&iloc);
  3288. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3289. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3290. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3291. EXT4_INODE_SIZE(inode->i_sb)) {
  3292. EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
  3293. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
  3294. EXT4_INODE_SIZE(inode->i_sb));
  3295. ret = -EIO;
  3296. goto bad_inode;
  3297. }
  3298. } else
  3299. ei->i_extra_isize = 0;
  3300. /* Precompute checksum seed for inode metadata */
  3301. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3302. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  3303. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3304. __u32 csum;
  3305. __le32 inum = cpu_to_le32(inode->i_ino);
  3306. __le32 gen = raw_inode->i_generation;
  3307. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  3308. sizeof(inum));
  3309. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  3310. sizeof(gen));
  3311. }
  3312. if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
  3313. EXT4_ERROR_INODE(inode, "checksum invalid");
  3314. ret = -EIO;
  3315. goto bad_inode;
  3316. }
  3317. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3318. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3319. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3320. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3321. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3322. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3323. }
  3324. i_uid_write(inode, i_uid);
  3325. i_gid_write(inode, i_gid);
  3326. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  3327. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  3328. ei->i_inline_off = 0;
  3329. ei->i_dir_start_lookup = 0;
  3330. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3331. /* We now have enough fields to check if the inode was active or not.
  3332. * This is needed because nfsd might try to access dead inodes
  3333. * the test is that same one that e2fsck uses
  3334. * NeilBrown 1999oct15
  3335. */
  3336. if (inode->i_nlink == 0) {
  3337. if (inode->i_mode == 0 ||
  3338. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  3339. /* this inode is deleted */
  3340. ret = -ESTALE;
  3341. goto bad_inode;
  3342. }
  3343. /* The only unlinked inodes we let through here have
  3344. * valid i_mode and are being read by the orphan
  3345. * recovery code: that's fine, we're about to complete
  3346. * the process of deleting those. */
  3347. }
  3348. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  3349. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  3350. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  3351. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
  3352. ei->i_file_acl |=
  3353. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  3354. inode->i_size = ext4_isize(raw_inode);
  3355. ei->i_disksize = inode->i_size;
  3356. #ifdef CONFIG_QUOTA
  3357. ei->i_reserved_quota = 0;
  3358. #endif
  3359. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  3360. ei->i_block_group = iloc.block_group;
  3361. ei->i_last_alloc_group = ~0;
  3362. /*
  3363. * NOTE! The in-memory inode i_data array is in little-endian order
  3364. * even on big-endian machines: we do NOT byteswap the block numbers!
  3365. */
  3366. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3367. ei->i_data[block] = raw_inode->i_block[block];
  3368. INIT_LIST_HEAD(&ei->i_orphan);
  3369. /*
  3370. * Set transaction id's of transactions that have to be committed
  3371. * to finish f[data]sync. We set them to currently running transaction
  3372. * as we cannot be sure that the inode or some of its metadata isn't
  3373. * part of the transaction - the inode could have been reclaimed and
  3374. * now it is reread from disk.
  3375. */
  3376. if (journal) {
  3377. transaction_t *transaction;
  3378. tid_t tid;
  3379. read_lock(&journal->j_state_lock);
  3380. if (journal->j_running_transaction)
  3381. transaction = journal->j_running_transaction;
  3382. else
  3383. transaction = journal->j_committing_transaction;
  3384. if (transaction)
  3385. tid = transaction->t_tid;
  3386. else
  3387. tid = journal->j_commit_sequence;
  3388. read_unlock(&journal->j_state_lock);
  3389. ei->i_sync_tid = tid;
  3390. ei->i_datasync_tid = tid;
  3391. }
  3392. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3393. if (ei->i_extra_isize == 0) {
  3394. /* The extra space is currently unused. Use it. */
  3395. ei->i_extra_isize = sizeof(struct ext4_inode) -
  3396. EXT4_GOOD_OLD_INODE_SIZE;
  3397. } else {
  3398. ext4_iget_extra_inode(inode, raw_inode, ei);
  3399. }
  3400. }
  3401. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  3402. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  3403. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  3404. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  3405. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  3406. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3407. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3408. inode->i_version |=
  3409. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  3410. }
  3411. ret = 0;
  3412. if (ei->i_file_acl &&
  3413. !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
  3414. EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
  3415. ei->i_file_acl);
  3416. ret = -EIO;
  3417. goto bad_inode;
  3418. } else if (!ext4_has_inline_data(inode)) {
  3419. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  3420. if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3421. (S_ISLNK(inode->i_mode) &&
  3422. !ext4_inode_is_fast_symlink(inode))))
  3423. /* Validate extent which is part of inode */
  3424. ret = ext4_ext_check_inode(inode);
  3425. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3426. (S_ISLNK(inode->i_mode) &&
  3427. !ext4_inode_is_fast_symlink(inode))) {
  3428. /* Validate block references which are part of inode */
  3429. ret = ext4_ind_check_inode(inode);
  3430. }
  3431. }
  3432. if (ret)
  3433. goto bad_inode;
  3434. if (S_ISREG(inode->i_mode)) {
  3435. inode->i_op = &ext4_file_inode_operations;
  3436. inode->i_fop = &ext4_file_operations;
  3437. ext4_set_aops(inode);
  3438. } else if (S_ISDIR(inode->i_mode)) {
  3439. inode->i_op = &ext4_dir_inode_operations;
  3440. inode->i_fop = &ext4_dir_operations;
  3441. } else if (S_ISLNK(inode->i_mode)) {
  3442. if (ext4_inode_is_fast_symlink(inode)) {
  3443. inode->i_op = &ext4_fast_symlink_inode_operations;
  3444. nd_terminate_link(ei->i_data, inode->i_size,
  3445. sizeof(ei->i_data) - 1);
  3446. } else {
  3447. inode->i_op = &ext4_symlink_inode_operations;
  3448. ext4_set_aops(inode);
  3449. }
  3450. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  3451. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  3452. inode->i_op = &ext4_special_inode_operations;
  3453. if (raw_inode->i_block[0])
  3454. init_special_inode(inode, inode->i_mode,
  3455. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  3456. else
  3457. init_special_inode(inode, inode->i_mode,
  3458. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  3459. } else {
  3460. ret = -EIO;
  3461. EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
  3462. goto bad_inode;
  3463. }
  3464. brelse(iloc.bh);
  3465. ext4_set_inode_flags(inode);
  3466. unlock_new_inode(inode);
  3467. return inode;
  3468. bad_inode:
  3469. brelse(iloc.bh);
  3470. iget_failed(inode);
  3471. return ERR_PTR(ret);
  3472. }
  3473. static int ext4_inode_blocks_set(handle_t *handle,
  3474. struct ext4_inode *raw_inode,
  3475. struct ext4_inode_info *ei)
  3476. {
  3477. struct inode *inode = &(ei->vfs_inode);
  3478. u64 i_blocks = inode->i_blocks;
  3479. struct super_block *sb = inode->i_sb;
  3480. if (i_blocks <= ~0U) {
  3481. /*
  3482. * i_blocks can be represented in a 32 bit variable
  3483. * as multiple of 512 bytes
  3484. */
  3485. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3486. raw_inode->i_blocks_high = 0;
  3487. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3488. return 0;
  3489. }
  3490. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
  3491. return -EFBIG;
  3492. if (i_blocks <= 0xffffffffffffULL) {
  3493. /*
  3494. * i_blocks can be represented in a 48 bit variable
  3495. * as multiple of 512 bytes
  3496. */
  3497. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3498. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3499. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3500. } else {
  3501. ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3502. /* i_block is stored in file system block size */
  3503. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  3504. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3505. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3506. }
  3507. return 0;
  3508. }
  3509. /*
  3510. * Post the struct inode info into an on-disk inode location in the
  3511. * buffer-cache. This gobbles the caller's reference to the
  3512. * buffer_head in the inode location struct.
  3513. *
  3514. * The caller must have write access to iloc->bh.
  3515. */
  3516. static int ext4_do_update_inode(handle_t *handle,
  3517. struct inode *inode,
  3518. struct ext4_iloc *iloc)
  3519. {
  3520. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  3521. struct ext4_inode_info *ei = EXT4_I(inode);
  3522. struct buffer_head *bh = iloc->bh;
  3523. int err = 0, rc, block;
  3524. int need_datasync = 0;
  3525. uid_t i_uid;
  3526. gid_t i_gid;
  3527. /* For fields not not tracking in the in-memory inode,
  3528. * initialise them to zero for new inodes. */
  3529. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  3530. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  3531. ext4_get_inode_flags(ei);
  3532. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  3533. i_uid = i_uid_read(inode);
  3534. i_gid = i_gid_read(inode);
  3535. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3536. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
  3537. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
  3538. /*
  3539. * Fix up interoperability with old kernels. Otherwise, old inodes get
  3540. * re-used with the upper 16 bits of the uid/gid intact
  3541. */
  3542. if (!ei->i_dtime) {
  3543. raw_inode->i_uid_high =
  3544. cpu_to_le16(high_16_bits(i_uid));
  3545. raw_inode->i_gid_high =
  3546. cpu_to_le16(high_16_bits(i_gid));
  3547. } else {
  3548. raw_inode->i_uid_high = 0;
  3549. raw_inode->i_gid_high = 0;
  3550. }
  3551. } else {
  3552. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
  3553. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
  3554. raw_inode->i_uid_high = 0;
  3555. raw_inode->i_gid_high = 0;
  3556. }
  3557. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  3558. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  3559. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  3560. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  3561. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  3562. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  3563. goto out_brelse;
  3564. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  3565. raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
  3566. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  3567. cpu_to_le32(EXT4_OS_HURD))
  3568. raw_inode->i_file_acl_high =
  3569. cpu_to_le16(ei->i_file_acl >> 32);
  3570. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  3571. if (ei->i_disksize != ext4_isize(raw_inode)) {
  3572. ext4_isize_set(raw_inode, ei->i_disksize);
  3573. need_datasync = 1;
  3574. }
  3575. if (ei->i_disksize > 0x7fffffffULL) {
  3576. struct super_block *sb = inode->i_sb;
  3577. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3578. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  3579. EXT4_SB(sb)->s_es->s_rev_level ==
  3580. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  3581. /* If this is the first large file
  3582. * created, add a flag to the superblock.
  3583. */
  3584. err = ext4_journal_get_write_access(handle,
  3585. EXT4_SB(sb)->s_sbh);
  3586. if (err)
  3587. goto out_brelse;
  3588. ext4_update_dynamic_rev(sb);
  3589. EXT4_SET_RO_COMPAT_FEATURE(sb,
  3590. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  3591. ext4_handle_sync(handle);
  3592. err = ext4_handle_dirty_super(handle, sb);
  3593. }
  3594. }
  3595. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  3596. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  3597. if (old_valid_dev(inode->i_rdev)) {
  3598. raw_inode->i_block[0] =
  3599. cpu_to_le32(old_encode_dev(inode->i_rdev));
  3600. raw_inode->i_block[1] = 0;
  3601. } else {
  3602. raw_inode->i_block[0] = 0;
  3603. raw_inode->i_block[1] =
  3604. cpu_to_le32(new_encode_dev(inode->i_rdev));
  3605. raw_inode->i_block[2] = 0;
  3606. }
  3607. } else if (!ext4_has_inline_data(inode)) {
  3608. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3609. raw_inode->i_block[block] = ei->i_data[block];
  3610. }
  3611. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  3612. if (ei->i_extra_isize) {
  3613. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3614. raw_inode->i_version_hi =
  3615. cpu_to_le32(inode->i_version >> 32);
  3616. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  3617. }
  3618. ext4_inode_csum_set(inode, raw_inode, ei);
  3619. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  3620. rc = ext4_handle_dirty_metadata(handle, NULL, bh);
  3621. if (!err)
  3622. err = rc;
  3623. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  3624. ext4_update_inode_fsync_trans(handle, inode, need_datasync);
  3625. out_brelse:
  3626. brelse(bh);
  3627. ext4_std_error(inode->i_sb, err);
  3628. return err;
  3629. }
  3630. /*
  3631. * ext4_write_inode()
  3632. *
  3633. * We are called from a few places:
  3634. *
  3635. * - Within generic_file_write() for O_SYNC files.
  3636. * Here, there will be no transaction running. We wait for any running
  3637. * transaction to commit.
  3638. *
  3639. * - Within sys_sync(), kupdate and such.
  3640. * We wait on commit, if tol to.
  3641. *
  3642. * - Within prune_icache() (PF_MEMALLOC == true)
  3643. * Here we simply return. We can't afford to block kswapd on the
  3644. * journal commit.
  3645. *
  3646. * In all cases it is actually safe for us to return without doing anything,
  3647. * because the inode has been copied into a raw inode buffer in
  3648. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  3649. * knfsd.
  3650. *
  3651. * Note that we are absolutely dependent upon all inode dirtiers doing the
  3652. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  3653. * which we are interested.
  3654. *
  3655. * It would be a bug for them to not do this. The code:
  3656. *
  3657. * mark_inode_dirty(inode)
  3658. * stuff();
  3659. * inode->i_size = expr;
  3660. *
  3661. * is in error because a kswapd-driven write_inode() could occur while
  3662. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  3663. * will no longer be on the superblock's dirty inode list.
  3664. */
  3665. int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
  3666. {
  3667. int err;
  3668. if (current->flags & PF_MEMALLOC)
  3669. return 0;
  3670. if (EXT4_SB(inode->i_sb)->s_journal) {
  3671. if (ext4_journal_current_handle()) {
  3672. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  3673. dump_stack();
  3674. return -EIO;
  3675. }
  3676. if (wbc->sync_mode != WB_SYNC_ALL)
  3677. return 0;
  3678. err = ext4_force_commit(inode->i_sb);
  3679. } else {
  3680. struct ext4_iloc iloc;
  3681. err = __ext4_get_inode_loc(inode, &iloc, 0);
  3682. if (err)
  3683. return err;
  3684. if (wbc->sync_mode == WB_SYNC_ALL)
  3685. sync_dirty_buffer(iloc.bh);
  3686. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  3687. EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
  3688. "IO error syncing inode");
  3689. err = -EIO;
  3690. }
  3691. brelse(iloc.bh);
  3692. }
  3693. return err;
  3694. }
  3695. /*
  3696. * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
  3697. * buffers that are attached to a page stradding i_size and are undergoing
  3698. * commit. In that case we have to wait for commit to finish and try again.
  3699. */
  3700. static void ext4_wait_for_tail_page_commit(struct inode *inode)
  3701. {
  3702. struct page *page;
  3703. unsigned offset;
  3704. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  3705. tid_t commit_tid = 0;
  3706. int ret;
  3707. offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
  3708. /*
  3709. * All buffers in the last page remain valid? Then there's nothing to
  3710. * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
  3711. * blocksize case
  3712. */
  3713. if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
  3714. return;
  3715. while (1) {
  3716. page = find_lock_page(inode->i_mapping,
  3717. inode->i_size >> PAGE_CACHE_SHIFT);
  3718. if (!page)
  3719. return;
  3720. ret = __ext4_journalled_invalidatepage(page, offset);
  3721. unlock_page(page);
  3722. page_cache_release(page);
  3723. if (ret != -EBUSY)
  3724. return;
  3725. commit_tid = 0;
  3726. read_lock(&journal->j_state_lock);
  3727. if (journal->j_committing_transaction)
  3728. commit_tid = journal->j_committing_transaction->t_tid;
  3729. read_unlock(&journal->j_state_lock);
  3730. if (commit_tid)
  3731. jbd2_log_wait_commit(journal, commit_tid);
  3732. }
  3733. }
  3734. /*
  3735. * ext4_setattr()
  3736. *
  3737. * Called from notify_change.
  3738. *
  3739. * We want to trap VFS attempts to truncate the file as soon as
  3740. * possible. In particular, we want to make sure that when the VFS
  3741. * shrinks i_size, we put the inode on the orphan list and modify
  3742. * i_disksize immediately, so that during the subsequent flushing of
  3743. * dirty pages and freeing of disk blocks, we can guarantee that any
  3744. * commit will leave the blocks being flushed in an unused state on
  3745. * disk. (On recovery, the inode will get truncated and the blocks will
  3746. * be freed, so we have a strong guarantee that no future commit will
  3747. * leave these blocks visible to the user.)
  3748. *
  3749. * Another thing we have to assure is that if we are in ordered mode
  3750. * and inode is still attached to the committing transaction, we must
  3751. * we start writeout of all the dirty pages which are being truncated.
  3752. * This way we are sure that all the data written in the previous
  3753. * transaction are already on disk (truncate waits for pages under
  3754. * writeback).
  3755. *
  3756. * Called with inode->i_mutex down.
  3757. */
  3758. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  3759. {
  3760. struct inode *inode = dentry->d_inode;
  3761. int error, rc = 0;
  3762. int orphan = 0;
  3763. const unsigned int ia_valid = attr->ia_valid;
  3764. error = inode_change_ok(inode, attr);
  3765. if (error)
  3766. return error;
  3767. if (is_quota_modification(inode, attr))
  3768. dquot_initialize(inode);
  3769. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  3770. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  3771. handle_t *handle;
  3772. /* (user+group)*(old+new) structure, inode write (sb,
  3773. * inode block, ? - but truncate inode update has it) */
  3774. handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
  3775. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
  3776. if (IS_ERR(handle)) {
  3777. error = PTR_ERR(handle);
  3778. goto err_out;
  3779. }
  3780. error = dquot_transfer(inode, attr);
  3781. if (error) {
  3782. ext4_journal_stop(handle);
  3783. return error;
  3784. }
  3785. /* Update corresponding info in inode so that everything is in
  3786. * one transaction */
  3787. if (attr->ia_valid & ATTR_UID)
  3788. inode->i_uid = attr->ia_uid;
  3789. if (attr->ia_valid & ATTR_GID)
  3790. inode->i_gid = attr->ia_gid;
  3791. error = ext4_mark_inode_dirty(handle, inode);
  3792. ext4_journal_stop(handle);
  3793. }
  3794. if (attr->ia_valid & ATTR_SIZE) {
  3795. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  3796. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3797. if (attr->ia_size > sbi->s_bitmap_maxbytes)
  3798. return -EFBIG;
  3799. }
  3800. }
  3801. if (S_ISREG(inode->i_mode) &&
  3802. attr->ia_valid & ATTR_SIZE &&
  3803. (attr->ia_size < inode->i_size)) {
  3804. handle_t *handle;
  3805. handle = ext4_journal_start(inode, 3);
  3806. if (IS_ERR(handle)) {
  3807. error = PTR_ERR(handle);
  3808. goto err_out;
  3809. }
  3810. if (ext4_handle_valid(handle)) {
  3811. error = ext4_orphan_add(handle, inode);
  3812. orphan = 1;
  3813. }
  3814. EXT4_I(inode)->i_disksize = attr->ia_size;
  3815. rc = ext4_mark_inode_dirty(handle, inode);
  3816. if (!error)
  3817. error = rc;
  3818. ext4_journal_stop(handle);
  3819. if (ext4_should_order_data(inode)) {
  3820. error = ext4_begin_ordered_truncate(inode,
  3821. attr->ia_size);
  3822. if (error) {
  3823. /* Do as much error cleanup as possible */
  3824. handle = ext4_journal_start(inode, 3);
  3825. if (IS_ERR(handle)) {
  3826. ext4_orphan_del(NULL, inode);
  3827. goto err_out;
  3828. }
  3829. ext4_orphan_del(handle, inode);
  3830. orphan = 0;
  3831. ext4_journal_stop(handle);
  3832. goto err_out;
  3833. }
  3834. }
  3835. }
  3836. if (attr->ia_valid & ATTR_SIZE) {
  3837. if (attr->ia_size != inode->i_size) {
  3838. loff_t oldsize = inode->i_size;
  3839. i_size_write(inode, attr->ia_size);
  3840. /*
  3841. * Blocks are going to be removed from the inode. Wait
  3842. * for dio in flight. Temporarily disable
  3843. * dioread_nolock to prevent livelock.
  3844. */
  3845. if (orphan) {
  3846. if (!ext4_should_journal_data(inode)) {
  3847. ext4_inode_block_unlocked_dio(inode);
  3848. inode_dio_wait(inode);
  3849. ext4_inode_resume_unlocked_dio(inode);
  3850. } else
  3851. ext4_wait_for_tail_page_commit(inode);
  3852. }
  3853. /*
  3854. * Truncate pagecache after we've waited for commit
  3855. * in data=journal mode to make pages freeable.
  3856. */
  3857. truncate_pagecache(inode, oldsize, inode->i_size);
  3858. }
  3859. ext4_truncate(inode);
  3860. }
  3861. if (!rc) {
  3862. setattr_copy(inode, attr);
  3863. mark_inode_dirty(inode);
  3864. }
  3865. /*
  3866. * If the call to ext4_truncate failed to get a transaction handle at
  3867. * all, we need to clean up the in-core orphan list manually.
  3868. */
  3869. if (orphan && inode->i_nlink)
  3870. ext4_orphan_del(NULL, inode);
  3871. if (!rc && (ia_valid & ATTR_MODE))
  3872. rc = ext4_acl_chmod(inode);
  3873. err_out:
  3874. ext4_std_error(inode->i_sb, error);
  3875. if (!error)
  3876. error = rc;
  3877. return error;
  3878. }
  3879. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  3880. struct kstat *stat)
  3881. {
  3882. struct inode *inode;
  3883. unsigned long delalloc_blocks;
  3884. inode = dentry->d_inode;
  3885. generic_fillattr(inode, stat);
  3886. /*
  3887. * We can't update i_blocks if the block allocation is delayed
  3888. * otherwise in the case of system crash before the real block
  3889. * allocation is done, we will have i_blocks inconsistent with
  3890. * on-disk file blocks.
  3891. * We always keep i_blocks updated together with real
  3892. * allocation. But to not confuse with user, stat
  3893. * will return the blocks that include the delayed allocation
  3894. * blocks for this file.
  3895. */
  3896. delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  3897. EXT4_I(inode)->i_reserved_data_blocks);
  3898. stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
  3899. return 0;
  3900. }
  3901. static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  3902. {
  3903. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  3904. return ext4_ind_trans_blocks(inode, nrblocks, chunk);
  3905. return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
  3906. }
  3907. /*
  3908. * Account for index blocks, block groups bitmaps and block group
  3909. * descriptor blocks if modify datablocks and index blocks
  3910. * worse case, the indexs blocks spread over different block groups
  3911. *
  3912. * If datablocks are discontiguous, they are possible to spread over
  3913. * different block groups too. If they are contiguous, with flexbg,
  3914. * they could still across block group boundary.
  3915. *
  3916. * Also account for superblock, inode, quota and xattr blocks
  3917. */
  3918. static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  3919. {
  3920. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  3921. int gdpblocks;
  3922. int idxblocks;
  3923. int ret = 0;
  3924. /*
  3925. * How many index blocks need to touch to modify nrblocks?
  3926. * The "Chunk" flag indicating whether the nrblocks is
  3927. * physically contiguous on disk
  3928. *
  3929. * For Direct IO and fallocate, they calls get_block to allocate
  3930. * one single extent at a time, so they could set the "Chunk" flag
  3931. */
  3932. idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
  3933. ret = idxblocks;
  3934. /*
  3935. * Now let's see how many group bitmaps and group descriptors need
  3936. * to account
  3937. */
  3938. groups = idxblocks;
  3939. if (chunk)
  3940. groups += 1;
  3941. else
  3942. groups += nrblocks;
  3943. gdpblocks = groups;
  3944. if (groups > ngroups)
  3945. groups = ngroups;
  3946. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  3947. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  3948. /* bitmaps and block group descriptor blocks */
  3949. ret += groups + gdpblocks;
  3950. /* Blocks for super block, inode, quota and xattr blocks */
  3951. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  3952. return ret;
  3953. }
  3954. /*
  3955. * Calculate the total number of credits to reserve to fit
  3956. * the modification of a single pages into a single transaction,
  3957. * which may include multiple chunks of block allocations.
  3958. *
  3959. * This could be called via ext4_write_begin()
  3960. *
  3961. * We need to consider the worse case, when
  3962. * one new block per extent.
  3963. */
  3964. int ext4_writepage_trans_blocks(struct inode *inode)
  3965. {
  3966. int bpp = ext4_journal_blocks_per_page(inode);
  3967. int ret;
  3968. ret = ext4_meta_trans_blocks(inode, bpp, 0);
  3969. /* Account for data blocks for journalled mode */
  3970. if (ext4_should_journal_data(inode))
  3971. ret += bpp;
  3972. return ret;
  3973. }
  3974. /*
  3975. * Calculate the journal credits for a chunk of data modification.
  3976. *
  3977. * This is called from DIO, fallocate or whoever calling
  3978. * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
  3979. *
  3980. * journal buffers for data blocks are not included here, as DIO
  3981. * and fallocate do no need to journal data buffers.
  3982. */
  3983. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  3984. {
  3985. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  3986. }
  3987. /*
  3988. * The caller must have previously called ext4_reserve_inode_write().
  3989. * Give this, we know that the caller already has write access to iloc->bh.
  3990. */
  3991. int ext4_mark_iloc_dirty(handle_t *handle,
  3992. struct inode *inode, struct ext4_iloc *iloc)
  3993. {
  3994. int err = 0;
  3995. if (IS_I_VERSION(inode))
  3996. inode_inc_iversion(inode);
  3997. /* the do_update_inode consumes one bh->b_count */
  3998. get_bh(iloc->bh);
  3999. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4000. err = ext4_do_update_inode(handle, inode, iloc);
  4001. put_bh(iloc->bh);
  4002. return err;
  4003. }
  4004. /*
  4005. * On success, We end up with an outstanding reference count against
  4006. * iloc->bh. This _must_ be cleaned up later.
  4007. */
  4008. int
  4009. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4010. struct ext4_iloc *iloc)
  4011. {
  4012. int err;
  4013. err = ext4_get_inode_loc(inode, iloc);
  4014. if (!err) {
  4015. BUFFER_TRACE(iloc->bh, "get_write_access");
  4016. err = ext4_journal_get_write_access(handle, iloc->bh);
  4017. if (err) {
  4018. brelse(iloc->bh);
  4019. iloc->bh = NULL;
  4020. }
  4021. }
  4022. ext4_std_error(inode->i_sb, err);
  4023. return err;
  4024. }
  4025. /*
  4026. * Expand an inode by new_extra_isize bytes.
  4027. * Returns 0 on success or negative error number on failure.
  4028. */
  4029. static int ext4_expand_extra_isize(struct inode *inode,
  4030. unsigned int new_extra_isize,
  4031. struct ext4_iloc iloc,
  4032. handle_t *handle)
  4033. {
  4034. struct ext4_inode *raw_inode;
  4035. struct ext4_xattr_ibody_header *header;
  4036. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4037. return 0;
  4038. raw_inode = ext4_raw_inode(&iloc);
  4039. header = IHDR(inode, raw_inode);
  4040. /* No extended attributes present */
  4041. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  4042. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4043. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4044. new_extra_isize);
  4045. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4046. return 0;
  4047. }
  4048. /* try to expand with EAs present */
  4049. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4050. raw_inode, handle);
  4051. }
  4052. /*
  4053. * What we do here is to mark the in-core inode as clean with respect to inode
  4054. * dirtiness (it may still be data-dirty).
  4055. * This means that the in-core inode may be reaped by prune_icache
  4056. * without having to perform any I/O. This is a very good thing,
  4057. * because *any* task may call prune_icache - even ones which
  4058. * have a transaction open against a different journal.
  4059. *
  4060. * Is this cheating? Not really. Sure, we haven't written the
  4061. * inode out, but prune_icache isn't a user-visible syncing function.
  4062. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4063. * we start and wait on commits.
  4064. */
  4065. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4066. {
  4067. struct ext4_iloc iloc;
  4068. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4069. static unsigned int mnt_count;
  4070. int err, ret;
  4071. might_sleep();
  4072. trace_ext4_mark_inode_dirty(inode, _RET_IP_);
  4073. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4074. if (ext4_handle_valid(handle) &&
  4075. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4076. !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
  4077. /*
  4078. * We need extra buffer credits since we may write into EA block
  4079. * with this same handle. If journal_extend fails, then it will
  4080. * only result in a minor loss of functionality for that inode.
  4081. * If this is felt to be critical, then e2fsck should be run to
  4082. * force a large enough s_min_extra_isize.
  4083. */
  4084. if ((jbd2_journal_extend(handle,
  4085. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4086. ret = ext4_expand_extra_isize(inode,
  4087. sbi->s_want_extra_isize,
  4088. iloc, handle);
  4089. if (ret) {
  4090. ext4_set_inode_state(inode,
  4091. EXT4_STATE_NO_EXPAND);
  4092. if (mnt_count !=
  4093. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4094. ext4_warning(inode->i_sb,
  4095. "Unable to expand inode %lu. Delete"
  4096. " some EAs or run e2fsck.",
  4097. inode->i_ino);
  4098. mnt_count =
  4099. le16_to_cpu(sbi->s_es->s_mnt_count);
  4100. }
  4101. }
  4102. }
  4103. }
  4104. if (!err)
  4105. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  4106. return err;
  4107. }
  4108. /*
  4109. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4110. *
  4111. * We're really interested in the case where a file is being extended.
  4112. * i_size has been changed by generic_commit_write() and we thus need
  4113. * to include the updated inode in the current transaction.
  4114. *
  4115. * Also, dquot_alloc_block() will always dirty the inode when blocks
  4116. * are allocated to the file.
  4117. *
  4118. * If the inode is marked synchronous, we don't honour that here - doing
  4119. * so would cause a commit on atime updates, which we don't bother doing.
  4120. * We handle synchronous inodes at the highest possible level.
  4121. */
  4122. void ext4_dirty_inode(struct inode *inode, int flags)
  4123. {
  4124. handle_t *handle;
  4125. handle = ext4_journal_start(inode, 2);
  4126. if (IS_ERR(handle))
  4127. goto out;
  4128. ext4_mark_inode_dirty(handle, inode);
  4129. ext4_journal_stop(handle);
  4130. out:
  4131. return;
  4132. }
  4133. #if 0
  4134. /*
  4135. * Bind an inode's backing buffer_head into this transaction, to prevent
  4136. * it from being flushed to disk early. Unlike
  4137. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4138. * returns no iloc structure, so the caller needs to repeat the iloc
  4139. * lookup to mark the inode dirty later.
  4140. */
  4141. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4142. {
  4143. struct ext4_iloc iloc;
  4144. int err = 0;
  4145. if (handle) {
  4146. err = ext4_get_inode_loc(inode, &iloc);
  4147. if (!err) {
  4148. BUFFER_TRACE(iloc.bh, "get_write_access");
  4149. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4150. if (!err)
  4151. err = ext4_handle_dirty_metadata(handle,
  4152. NULL,
  4153. iloc.bh);
  4154. brelse(iloc.bh);
  4155. }
  4156. }
  4157. ext4_std_error(inode->i_sb, err);
  4158. return err;
  4159. }
  4160. #endif
  4161. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4162. {
  4163. journal_t *journal;
  4164. handle_t *handle;
  4165. int err;
  4166. /*
  4167. * We have to be very careful here: changing a data block's
  4168. * journaling status dynamically is dangerous. If we write a
  4169. * data block to the journal, change the status and then delete
  4170. * that block, we risk forgetting to revoke the old log record
  4171. * from the journal and so a subsequent replay can corrupt data.
  4172. * So, first we make sure that the journal is empty and that
  4173. * nobody is changing anything.
  4174. */
  4175. journal = EXT4_JOURNAL(inode);
  4176. if (!journal)
  4177. return 0;
  4178. if (is_journal_aborted(journal))
  4179. return -EROFS;
  4180. /* We have to allocate physical blocks for delalloc blocks
  4181. * before flushing journal. otherwise delalloc blocks can not
  4182. * be allocated any more. even more truncate on delalloc blocks
  4183. * could trigger BUG by flushing delalloc blocks in journal.
  4184. * There is no delalloc block in non-journal data mode.
  4185. */
  4186. if (val && test_opt(inode->i_sb, DELALLOC)) {
  4187. err = ext4_alloc_da_blocks(inode);
  4188. if (err < 0)
  4189. return err;
  4190. }
  4191. /* Wait for all existing dio workers */
  4192. ext4_inode_block_unlocked_dio(inode);
  4193. inode_dio_wait(inode);
  4194. jbd2_journal_lock_updates(journal);
  4195. /*
  4196. * OK, there are no updates running now, and all cached data is
  4197. * synced to disk. We are now in a completely consistent state
  4198. * which doesn't have anything in the journal, and we know that
  4199. * no filesystem updates are running, so it is safe to modify
  4200. * the inode's in-core data-journaling state flag now.
  4201. */
  4202. if (val)
  4203. ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4204. else {
  4205. jbd2_journal_flush(journal);
  4206. ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4207. }
  4208. ext4_set_aops(inode);
  4209. jbd2_journal_unlock_updates(journal);
  4210. ext4_inode_resume_unlocked_dio(inode);
  4211. /* Finally we can mark the inode as dirty. */
  4212. handle = ext4_journal_start(inode, 1);
  4213. if (IS_ERR(handle))
  4214. return PTR_ERR(handle);
  4215. err = ext4_mark_inode_dirty(handle, inode);
  4216. ext4_handle_sync(handle);
  4217. ext4_journal_stop(handle);
  4218. ext4_std_error(inode->i_sb, err);
  4219. return err;
  4220. }
  4221. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  4222. {
  4223. return !buffer_mapped(bh);
  4224. }
  4225. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4226. {
  4227. struct page *page = vmf->page;
  4228. loff_t size;
  4229. unsigned long len;
  4230. int ret;
  4231. struct file *file = vma->vm_file;
  4232. struct inode *inode = file->f_path.dentry->d_inode;
  4233. struct address_space *mapping = inode->i_mapping;
  4234. handle_t *handle;
  4235. get_block_t *get_block;
  4236. int retries = 0;
  4237. sb_start_pagefault(inode->i_sb);
  4238. file_update_time(vma->vm_file);
  4239. /* Delalloc case is easy... */
  4240. if (test_opt(inode->i_sb, DELALLOC) &&
  4241. !ext4_should_journal_data(inode) &&
  4242. !ext4_nonda_switch(inode->i_sb)) {
  4243. do {
  4244. ret = __block_page_mkwrite(vma, vmf,
  4245. ext4_da_get_block_prep);
  4246. } while (ret == -ENOSPC &&
  4247. ext4_should_retry_alloc(inode->i_sb, &retries));
  4248. goto out_ret;
  4249. }
  4250. lock_page(page);
  4251. size = i_size_read(inode);
  4252. /* Page got truncated from under us? */
  4253. if (page->mapping != mapping || page_offset(page) > size) {
  4254. unlock_page(page);
  4255. ret = VM_FAULT_NOPAGE;
  4256. goto out;
  4257. }
  4258. if (page->index == size >> PAGE_CACHE_SHIFT)
  4259. len = size & ~PAGE_CACHE_MASK;
  4260. else
  4261. len = PAGE_CACHE_SIZE;
  4262. /*
  4263. * Return if we have all the buffers mapped. This avoids the need to do
  4264. * journal_start/journal_stop which can block and take a long time
  4265. */
  4266. if (page_has_buffers(page)) {
  4267. if (!ext4_walk_page_buffers(NULL, page_buffers(page),
  4268. 0, len, NULL,
  4269. ext4_bh_unmapped)) {
  4270. /* Wait so that we don't change page under IO */
  4271. wait_on_page_writeback(page);
  4272. ret = VM_FAULT_LOCKED;
  4273. goto out;
  4274. }
  4275. }
  4276. unlock_page(page);
  4277. /* OK, we need to fill the hole... */
  4278. if (ext4_should_dioread_nolock(inode))
  4279. get_block = ext4_get_block_write;
  4280. else
  4281. get_block = ext4_get_block;
  4282. retry_alloc:
  4283. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  4284. if (IS_ERR(handle)) {
  4285. ret = VM_FAULT_SIGBUS;
  4286. goto out;
  4287. }
  4288. ret = __block_page_mkwrite(vma, vmf, get_block);
  4289. if (!ret && ext4_should_journal_data(inode)) {
  4290. if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
  4291. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
  4292. unlock_page(page);
  4293. ret = VM_FAULT_SIGBUS;
  4294. ext4_journal_stop(handle);
  4295. goto out;
  4296. }
  4297. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  4298. }
  4299. ext4_journal_stop(handle);
  4300. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  4301. goto retry_alloc;
  4302. out_ret:
  4303. ret = block_page_mkwrite_return(ret);
  4304. out:
  4305. sb_end_pagefault(inode->i_sb);
  4306. return ret;
  4307. }