inode.c 136 KB

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