inode.c 138 KB

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