inode.c 138 KB

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