inode.c 139 KB

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