inode.c 127 KB

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