inode.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977
  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. if (mdb_free) {
  934. /* Account for allocated meta_blocks */
  935. mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
  936. /* update fs dirty blocks counter */
  937. percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
  938. EXT4_I(inode)->i_allocated_meta_blocks = 0;
  939. EXT4_I(inode)->i_reserved_meta_blocks = mdb;
  940. }
  941. /* update per-inode reservations */
  942. BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
  943. EXT4_I(inode)->i_reserved_data_blocks -= used;
  944. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  945. }
  946. /*
  947. * The ext4_get_blocks_wrap() function try to look up the requested blocks,
  948. * and returns if the blocks are already mapped.
  949. *
  950. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  951. * and store the allocated blocks in the result buffer head and mark it
  952. * mapped.
  953. *
  954. * If file type is extents based, it will call ext4_ext_get_blocks(),
  955. * Otherwise, call with ext4_get_blocks_handle() to handle indirect mapping
  956. * based files
  957. *
  958. * On success, it returns the number of blocks being mapped or allocate.
  959. * if create==0 and the blocks are pre-allocated and uninitialized block,
  960. * the result buffer head is unmapped. If the create ==1, it will make sure
  961. * the buffer head is mapped.
  962. *
  963. * It returns 0 if plain look up failed (blocks have not been allocated), in
  964. * that casem, buffer head is unmapped
  965. *
  966. * It returns the error in case of allocation failure.
  967. */
  968. int ext4_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
  969. unsigned long max_blocks, struct buffer_head *bh,
  970. int create, int extend_disksize, int flag)
  971. {
  972. int retval;
  973. clear_buffer_mapped(bh);
  974. /*
  975. * Try to see if we can get the block without requesting
  976. * for new file system block.
  977. */
  978. down_read((&EXT4_I(inode)->i_data_sem));
  979. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  980. retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
  981. bh, 0, 0);
  982. } else {
  983. retval = ext4_get_blocks_handle(handle,
  984. inode, block, max_blocks, bh, 0, 0);
  985. }
  986. up_read((&EXT4_I(inode)->i_data_sem));
  987. /* If it is only a block(s) look up */
  988. if (!create)
  989. return retval;
  990. /*
  991. * Returns if the blocks have already allocated
  992. *
  993. * Note that if blocks have been preallocated
  994. * ext4_ext_get_block() returns th create = 0
  995. * with buffer head unmapped.
  996. */
  997. if (retval > 0 && buffer_mapped(bh))
  998. return retval;
  999. /*
  1000. * New blocks allocate and/or writing to uninitialized extent
  1001. * will possibly result in updating i_data, so we take
  1002. * the write lock of i_data_sem, and call get_blocks()
  1003. * with create == 1 flag.
  1004. */
  1005. down_write((&EXT4_I(inode)->i_data_sem));
  1006. /*
  1007. * if the caller is from delayed allocation writeout path
  1008. * we have already reserved fs blocks for allocation
  1009. * let the underlying get_block() function know to
  1010. * avoid double accounting
  1011. */
  1012. if (flag)
  1013. EXT4_I(inode)->i_delalloc_reserved_flag = 1;
  1014. /*
  1015. * We need to check for EXT4 here because migrate
  1016. * could have changed the inode type in between
  1017. */
  1018. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  1019. retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
  1020. bh, create, extend_disksize);
  1021. } else {
  1022. retval = ext4_get_blocks_handle(handle, inode, block,
  1023. max_blocks, bh, create, extend_disksize);
  1024. if (retval > 0 && buffer_new(bh)) {
  1025. /*
  1026. * We allocated new blocks which will result in
  1027. * i_data's format changing. Force the migrate
  1028. * to fail by clearing migrate flags
  1029. */
  1030. EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
  1031. ~EXT4_EXT_MIGRATE;
  1032. }
  1033. }
  1034. if (flag) {
  1035. EXT4_I(inode)->i_delalloc_reserved_flag = 0;
  1036. /*
  1037. * Update reserved blocks/metadata blocks
  1038. * after successful block allocation
  1039. * which were deferred till now
  1040. */
  1041. if ((retval > 0) && buffer_delay(bh))
  1042. ext4_da_update_reserve_space(inode, retval);
  1043. }
  1044. up_write((&EXT4_I(inode)->i_data_sem));
  1045. return retval;
  1046. }
  1047. /* Maximum number of blocks we map for direct IO at once. */
  1048. #define DIO_MAX_BLOCKS 4096
  1049. static int ext4_get_block(struct inode *inode, sector_t iblock,
  1050. struct buffer_head *bh_result, int create)
  1051. {
  1052. handle_t *handle = ext4_journal_current_handle();
  1053. int ret = 0, started = 0;
  1054. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  1055. int dio_credits;
  1056. if (create && !handle) {
  1057. /* Direct IO write... */
  1058. if (max_blocks > DIO_MAX_BLOCKS)
  1059. max_blocks = DIO_MAX_BLOCKS;
  1060. dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
  1061. handle = ext4_journal_start(inode, dio_credits);
  1062. if (IS_ERR(handle)) {
  1063. ret = PTR_ERR(handle);
  1064. goto out;
  1065. }
  1066. started = 1;
  1067. }
  1068. ret = ext4_get_blocks_wrap(handle, inode, iblock,
  1069. max_blocks, bh_result, create, 0, 0);
  1070. if (ret > 0) {
  1071. bh_result->b_size = (ret << inode->i_blkbits);
  1072. ret = 0;
  1073. }
  1074. if (started)
  1075. ext4_journal_stop(handle);
  1076. out:
  1077. return ret;
  1078. }
  1079. /*
  1080. * `handle' can be NULL if create is zero
  1081. */
  1082. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  1083. ext4_lblk_t block, int create, int *errp)
  1084. {
  1085. struct buffer_head dummy;
  1086. int fatal = 0, err;
  1087. J_ASSERT(handle != NULL || create == 0);
  1088. dummy.b_state = 0;
  1089. dummy.b_blocknr = -1000;
  1090. buffer_trace_init(&dummy.b_history);
  1091. err = ext4_get_blocks_wrap(handle, inode, block, 1,
  1092. &dummy, create, 1, 0);
  1093. /*
  1094. * ext4_get_blocks_handle() returns number of blocks
  1095. * mapped. 0 in case of a HOLE.
  1096. */
  1097. if (err > 0) {
  1098. if (err > 1)
  1099. WARN_ON(1);
  1100. err = 0;
  1101. }
  1102. *errp = err;
  1103. if (!err && buffer_mapped(&dummy)) {
  1104. struct buffer_head *bh;
  1105. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  1106. if (!bh) {
  1107. *errp = -EIO;
  1108. goto err;
  1109. }
  1110. if (buffer_new(&dummy)) {
  1111. J_ASSERT(create != 0);
  1112. J_ASSERT(handle != NULL);
  1113. /*
  1114. * Now that we do not always journal data, we should
  1115. * keep in mind whether this should always journal the
  1116. * new buffer as metadata. For now, regular file
  1117. * writes use ext4_get_block instead, so it's not a
  1118. * problem.
  1119. */
  1120. lock_buffer(bh);
  1121. BUFFER_TRACE(bh, "call get_create_access");
  1122. fatal = ext4_journal_get_create_access(handle, bh);
  1123. if (!fatal && !buffer_uptodate(bh)) {
  1124. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1125. set_buffer_uptodate(bh);
  1126. }
  1127. unlock_buffer(bh);
  1128. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  1129. err = ext4_journal_dirty_metadata(handle, bh);
  1130. if (!fatal)
  1131. fatal = err;
  1132. } else {
  1133. BUFFER_TRACE(bh, "not a new buffer");
  1134. }
  1135. if (fatal) {
  1136. *errp = fatal;
  1137. brelse(bh);
  1138. bh = NULL;
  1139. }
  1140. return bh;
  1141. }
  1142. err:
  1143. return NULL;
  1144. }
  1145. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  1146. ext4_lblk_t block, int create, int *err)
  1147. {
  1148. struct buffer_head *bh;
  1149. bh = ext4_getblk(handle, inode, block, create, err);
  1150. if (!bh)
  1151. return bh;
  1152. if (buffer_uptodate(bh))
  1153. return bh;
  1154. ll_rw_block(READ_META, 1, &bh);
  1155. wait_on_buffer(bh);
  1156. if (buffer_uptodate(bh))
  1157. return bh;
  1158. put_bh(bh);
  1159. *err = -EIO;
  1160. return NULL;
  1161. }
  1162. static int walk_page_buffers(handle_t *handle,
  1163. struct buffer_head *head,
  1164. unsigned from,
  1165. unsigned to,
  1166. int *partial,
  1167. int (*fn)(handle_t *handle,
  1168. struct buffer_head *bh))
  1169. {
  1170. struct buffer_head *bh;
  1171. unsigned block_start, block_end;
  1172. unsigned blocksize = head->b_size;
  1173. int err, ret = 0;
  1174. struct buffer_head *next;
  1175. for (bh = head, block_start = 0;
  1176. ret == 0 && (bh != head || !block_start);
  1177. block_start = block_end, bh = next)
  1178. {
  1179. next = bh->b_this_page;
  1180. block_end = block_start + blocksize;
  1181. if (block_end <= from || block_start >= to) {
  1182. if (partial && !buffer_uptodate(bh))
  1183. *partial = 1;
  1184. continue;
  1185. }
  1186. err = (*fn)(handle, bh);
  1187. if (!ret)
  1188. ret = err;
  1189. }
  1190. return ret;
  1191. }
  1192. /*
  1193. * To preserve ordering, it is essential that the hole instantiation and
  1194. * the data write be encapsulated in a single transaction. We cannot
  1195. * close off a transaction and start a new one between the ext4_get_block()
  1196. * and the commit_write(). So doing the jbd2_journal_start at the start of
  1197. * prepare_write() is the right place.
  1198. *
  1199. * Also, this function can nest inside ext4_writepage() ->
  1200. * block_write_full_page(). In that case, we *know* that ext4_writepage()
  1201. * has generated enough buffer credits to do the whole page. So we won't
  1202. * block on the journal in that case, which is good, because the caller may
  1203. * be PF_MEMALLOC.
  1204. *
  1205. * By accident, ext4 can be reentered when a transaction is open via
  1206. * quota file writes. If we were to commit the transaction while thus
  1207. * reentered, there can be a deadlock - we would be holding a quota
  1208. * lock, and the commit would never complete if another thread had a
  1209. * transaction open and was blocking on the quota lock - a ranking
  1210. * violation.
  1211. *
  1212. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  1213. * will _not_ run commit under these circumstances because handle->h_ref
  1214. * is elevated. We'll still have enough credits for the tiny quotafile
  1215. * write.
  1216. */
  1217. static int do_journal_get_write_access(handle_t *handle,
  1218. struct buffer_head *bh)
  1219. {
  1220. if (!buffer_mapped(bh) || buffer_freed(bh))
  1221. return 0;
  1222. return ext4_journal_get_write_access(handle, bh);
  1223. }
  1224. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  1225. loff_t pos, unsigned len, unsigned flags,
  1226. struct page **pagep, void **fsdata)
  1227. {
  1228. struct inode *inode = mapping->host;
  1229. int ret, needed_blocks = ext4_writepage_trans_blocks(inode);
  1230. handle_t *handle;
  1231. int retries = 0;
  1232. struct page *page;
  1233. pgoff_t index;
  1234. unsigned from, to;
  1235. index = pos >> PAGE_CACHE_SHIFT;
  1236. from = pos & (PAGE_CACHE_SIZE - 1);
  1237. to = from + len;
  1238. retry:
  1239. handle = ext4_journal_start(inode, needed_blocks);
  1240. if (IS_ERR(handle)) {
  1241. ret = PTR_ERR(handle);
  1242. goto out;
  1243. }
  1244. page = __grab_cache_page(mapping, index);
  1245. if (!page) {
  1246. ext4_journal_stop(handle);
  1247. ret = -ENOMEM;
  1248. goto out;
  1249. }
  1250. *pagep = page;
  1251. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1252. ext4_get_block);
  1253. if (!ret && ext4_should_journal_data(inode)) {
  1254. ret = walk_page_buffers(handle, page_buffers(page),
  1255. from, to, NULL, do_journal_get_write_access);
  1256. }
  1257. if (ret) {
  1258. unlock_page(page);
  1259. ext4_journal_stop(handle);
  1260. page_cache_release(page);
  1261. }
  1262. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  1263. goto retry;
  1264. out:
  1265. return ret;
  1266. }
  1267. /* For write_end() in data=journal mode */
  1268. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  1269. {
  1270. if (!buffer_mapped(bh) || buffer_freed(bh))
  1271. return 0;
  1272. set_buffer_uptodate(bh);
  1273. return ext4_journal_dirty_metadata(handle, bh);
  1274. }
  1275. /*
  1276. * We need to pick up the new inode size which generic_commit_write gave us
  1277. * `file' can be NULL - eg, when called from page_symlink().
  1278. *
  1279. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  1280. * buffers are managed internally.
  1281. */
  1282. static int ext4_ordered_write_end(struct file *file,
  1283. struct address_space *mapping,
  1284. loff_t pos, unsigned len, unsigned copied,
  1285. struct page *page, void *fsdata)
  1286. {
  1287. handle_t *handle = ext4_journal_current_handle();
  1288. struct inode *inode = mapping->host;
  1289. int ret = 0, ret2;
  1290. ret = ext4_jbd2_file_inode(handle, inode);
  1291. if (ret == 0) {
  1292. /*
  1293. * generic_write_end() will run mark_inode_dirty() if i_size
  1294. * changes. So let's piggyback the i_disksize mark_inode_dirty
  1295. * into that.
  1296. */
  1297. loff_t new_i_size;
  1298. new_i_size = pos + copied;
  1299. if (new_i_size > EXT4_I(inode)->i_disksize)
  1300. EXT4_I(inode)->i_disksize = new_i_size;
  1301. ret2 = generic_write_end(file, mapping, pos, len, copied,
  1302. page, fsdata);
  1303. copied = ret2;
  1304. if (ret2 < 0)
  1305. ret = ret2;
  1306. }
  1307. ret2 = ext4_journal_stop(handle);
  1308. if (!ret)
  1309. ret = ret2;
  1310. return ret ? ret : copied;
  1311. }
  1312. static int ext4_writeback_write_end(struct file *file,
  1313. struct address_space *mapping,
  1314. loff_t pos, unsigned len, unsigned copied,
  1315. struct page *page, void *fsdata)
  1316. {
  1317. handle_t *handle = ext4_journal_current_handle();
  1318. struct inode *inode = mapping->host;
  1319. int ret = 0, ret2;
  1320. loff_t new_i_size;
  1321. new_i_size = pos + copied;
  1322. if (new_i_size > EXT4_I(inode)->i_disksize)
  1323. EXT4_I(inode)->i_disksize = new_i_size;
  1324. ret2 = generic_write_end(file, mapping, pos, len, copied,
  1325. page, fsdata);
  1326. copied = ret2;
  1327. if (ret2 < 0)
  1328. ret = ret2;
  1329. ret2 = ext4_journal_stop(handle);
  1330. if (!ret)
  1331. ret = ret2;
  1332. return ret ? ret : copied;
  1333. }
  1334. static int ext4_journalled_write_end(struct file *file,
  1335. struct address_space *mapping,
  1336. loff_t pos, unsigned len, unsigned copied,
  1337. struct page *page, void *fsdata)
  1338. {
  1339. handle_t *handle = ext4_journal_current_handle();
  1340. struct inode *inode = mapping->host;
  1341. int ret = 0, ret2;
  1342. int partial = 0;
  1343. unsigned from, to;
  1344. from = pos & (PAGE_CACHE_SIZE - 1);
  1345. to = from + len;
  1346. if (copied < len) {
  1347. if (!PageUptodate(page))
  1348. copied = 0;
  1349. page_zero_new_buffers(page, from+copied, to);
  1350. }
  1351. ret = walk_page_buffers(handle, page_buffers(page), from,
  1352. to, &partial, write_end_fn);
  1353. if (!partial)
  1354. SetPageUptodate(page);
  1355. if (pos+copied > inode->i_size)
  1356. i_size_write(inode, pos+copied);
  1357. EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
  1358. if (inode->i_size > EXT4_I(inode)->i_disksize) {
  1359. EXT4_I(inode)->i_disksize = inode->i_size;
  1360. ret2 = ext4_mark_inode_dirty(handle, inode);
  1361. if (!ret)
  1362. ret = ret2;
  1363. }
  1364. unlock_page(page);
  1365. ret2 = ext4_journal_stop(handle);
  1366. if (!ret)
  1367. ret = ret2;
  1368. page_cache_release(page);
  1369. return ret ? ret : copied;
  1370. }
  1371. static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
  1372. {
  1373. int retries = 0;
  1374. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1375. unsigned long md_needed, mdblocks, total = 0;
  1376. /*
  1377. * recalculate the amount of metadata blocks to reserve
  1378. * in order to allocate nrblocks
  1379. * worse case is one extent per block
  1380. */
  1381. repeat:
  1382. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1383. total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
  1384. mdblocks = ext4_calc_metadata_amount(inode, total);
  1385. BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
  1386. md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
  1387. total = md_needed + nrblocks;
  1388. if (ext4_claim_free_blocks(sbi, total)) {
  1389. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1390. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  1391. yield();
  1392. goto repeat;
  1393. }
  1394. return -ENOSPC;
  1395. }
  1396. EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
  1397. EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
  1398. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1399. return 0; /* success */
  1400. }
  1401. static void ext4_da_release_space(struct inode *inode, int to_free)
  1402. {
  1403. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1404. int total, mdb, mdb_free, release;
  1405. if (!to_free)
  1406. return; /* Nothing to release, exit */
  1407. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1408. if (!EXT4_I(inode)->i_reserved_data_blocks) {
  1409. /*
  1410. * if there is no reserved blocks, but we try to free some
  1411. * then the counter is messed up somewhere.
  1412. * but since this function is called from invalidate
  1413. * page, it's harmless to return without any action
  1414. */
  1415. printk(KERN_INFO "ext4 delalloc try to release %d reserved "
  1416. "blocks for inode %lu, but there is no reserved "
  1417. "data blocks\n", to_free, inode->i_ino);
  1418. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1419. return;
  1420. }
  1421. /* recalculate the number of metablocks still need to be reserved */
  1422. total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
  1423. mdb = ext4_calc_metadata_amount(inode, total);
  1424. /* figure out how many metablocks to release */
  1425. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  1426. mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
  1427. release = to_free + mdb_free;
  1428. /* update fs dirty blocks counter for truncate case */
  1429. percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
  1430. /* update per-inode reservations */
  1431. BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
  1432. EXT4_I(inode)->i_reserved_data_blocks -= to_free;
  1433. BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
  1434. EXT4_I(inode)->i_reserved_meta_blocks = mdb;
  1435. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1436. }
  1437. static void ext4_da_page_release_reservation(struct page *page,
  1438. unsigned long offset)
  1439. {
  1440. int to_release = 0;
  1441. struct buffer_head *head, *bh;
  1442. unsigned int curr_off = 0;
  1443. head = page_buffers(page);
  1444. bh = head;
  1445. do {
  1446. unsigned int next_off = curr_off + bh->b_size;
  1447. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1448. to_release++;
  1449. clear_buffer_delay(bh);
  1450. }
  1451. curr_off = next_off;
  1452. } while ((bh = bh->b_this_page) != head);
  1453. ext4_da_release_space(page->mapping->host, to_release);
  1454. }
  1455. /*
  1456. * Delayed allocation stuff
  1457. */
  1458. struct mpage_da_data {
  1459. struct inode *inode;
  1460. struct buffer_head lbh; /* extent of blocks */
  1461. unsigned long first_page, next_page; /* extent of pages */
  1462. get_block_t *get_block;
  1463. struct writeback_control *wbc;
  1464. int io_done;
  1465. long pages_written;
  1466. };
  1467. /*
  1468. * mpage_da_submit_io - walks through extent of pages and try to write
  1469. * them with writepage() call back
  1470. *
  1471. * @mpd->inode: inode
  1472. * @mpd->first_page: first page of the extent
  1473. * @mpd->next_page: page after the last page of the extent
  1474. * @mpd->get_block: the filesystem's block mapper function
  1475. *
  1476. * By the time mpage_da_submit_io() is called we expect all blocks
  1477. * to be allocated. this may be wrong if allocation failed.
  1478. *
  1479. * As pages are already locked by write_cache_pages(), we can't use it
  1480. */
  1481. static int mpage_da_submit_io(struct mpage_da_data *mpd)
  1482. {
  1483. struct address_space *mapping = mpd->inode->i_mapping;
  1484. int ret = 0, err, nr_pages, i;
  1485. unsigned long index, end;
  1486. struct pagevec pvec;
  1487. BUG_ON(mpd->next_page <= mpd->first_page);
  1488. pagevec_init(&pvec, 0);
  1489. index = mpd->first_page;
  1490. end = mpd->next_page - 1;
  1491. while (index <= end) {
  1492. /* XXX: optimize tail */
  1493. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1494. if (nr_pages == 0)
  1495. break;
  1496. for (i = 0; i < nr_pages; i++) {
  1497. struct page *page = pvec.pages[i];
  1498. index = page->index;
  1499. if (index > end)
  1500. break;
  1501. index++;
  1502. err = mapping->a_ops->writepage(page, mpd->wbc);
  1503. if (!err)
  1504. mpd->pages_written++;
  1505. /*
  1506. * In error case, we have to continue because
  1507. * remaining pages are still locked
  1508. * XXX: unlock and re-dirty them?
  1509. */
  1510. if (ret == 0)
  1511. ret = err;
  1512. }
  1513. pagevec_release(&pvec);
  1514. }
  1515. return ret;
  1516. }
  1517. /*
  1518. * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
  1519. *
  1520. * @mpd->inode - inode to walk through
  1521. * @exbh->b_blocknr - first block on a disk
  1522. * @exbh->b_size - amount of space in bytes
  1523. * @logical - first logical block to start assignment with
  1524. *
  1525. * the function goes through all passed space and put actual disk
  1526. * block numbers into buffer heads, dropping BH_Delay
  1527. */
  1528. static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
  1529. struct buffer_head *exbh)
  1530. {
  1531. struct inode *inode = mpd->inode;
  1532. struct address_space *mapping = inode->i_mapping;
  1533. int blocks = exbh->b_size >> inode->i_blkbits;
  1534. sector_t pblock = exbh->b_blocknr, cur_logical;
  1535. struct buffer_head *head, *bh;
  1536. pgoff_t index, end;
  1537. struct pagevec pvec;
  1538. int nr_pages, i;
  1539. index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1540. end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1541. cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1542. pagevec_init(&pvec, 0);
  1543. while (index <= end) {
  1544. /* XXX: optimize tail */
  1545. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1546. if (nr_pages == 0)
  1547. break;
  1548. for (i = 0; i < nr_pages; i++) {
  1549. struct page *page = pvec.pages[i];
  1550. index = page->index;
  1551. if (index > end)
  1552. break;
  1553. index++;
  1554. BUG_ON(!PageLocked(page));
  1555. BUG_ON(PageWriteback(page));
  1556. BUG_ON(!page_has_buffers(page));
  1557. bh = page_buffers(page);
  1558. head = bh;
  1559. /* skip blocks out of the range */
  1560. do {
  1561. if (cur_logical >= logical)
  1562. break;
  1563. cur_logical++;
  1564. } while ((bh = bh->b_this_page) != head);
  1565. do {
  1566. if (cur_logical >= logical + blocks)
  1567. break;
  1568. if (buffer_delay(bh)) {
  1569. bh->b_blocknr = pblock;
  1570. clear_buffer_delay(bh);
  1571. bh->b_bdev = inode->i_sb->s_bdev;
  1572. } else if (buffer_unwritten(bh)) {
  1573. bh->b_blocknr = pblock;
  1574. clear_buffer_unwritten(bh);
  1575. set_buffer_mapped(bh);
  1576. set_buffer_new(bh);
  1577. bh->b_bdev = inode->i_sb->s_bdev;
  1578. } else if (buffer_mapped(bh))
  1579. BUG_ON(bh->b_blocknr != pblock);
  1580. cur_logical++;
  1581. pblock++;
  1582. } while ((bh = bh->b_this_page) != head);
  1583. }
  1584. pagevec_release(&pvec);
  1585. }
  1586. }
  1587. /*
  1588. * __unmap_underlying_blocks - just a helper function to unmap
  1589. * set of blocks described by @bh
  1590. */
  1591. static inline void __unmap_underlying_blocks(struct inode *inode,
  1592. struct buffer_head *bh)
  1593. {
  1594. struct block_device *bdev = inode->i_sb->s_bdev;
  1595. int blocks, i;
  1596. blocks = bh->b_size >> inode->i_blkbits;
  1597. for (i = 0; i < blocks; i++)
  1598. unmap_underlying_metadata(bdev, bh->b_blocknr + i);
  1599. }
  1600. static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
  1601. sector_t logical, long blk_cnt)
  1602. {
  1603. int nr_pages, i;
  1604. pgoff_t index, end;
  1605. struct pagevec pvec;
  1606. struct inode *inode = mpd->inode;
  1607. struct address_space *mapping = inode->i_mapping;
  1608. index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1609. end = (logical + blk_cnt - 1) >>
  1610. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1611. while (index <= end) {
  1612. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1613. if (nr_pages == 0)
  1614. break;
  1615. for (i = 0; i < nr_pages; i++) {
  1616. struct page *page = pvec.pages[i];
  1617. index = page->index;
  1618. if (index > end)
  1619. break;
  1620. index++;
  1621. BUG_ON(!PageLocked(page));
  1622. BUG_ON(PageWriteback(page));
  1623. block_invalidatepage(page, 0);
  1624. ClearPageUptodate(page);
  1625. unlock_page(page);
  1626. }
  1627. }
  1628. return;
  1629. }
  1630. /*
  1631. * mpage_da_map_blocks - go through given space
  1632. *
  1633. * @mpd->lbh - bh describing space
  1634. * @mpd->get_block - the filesystem's block mapper function
  1635. *
  1636. * The function skips space we know is already mapped to disk blocks.
  1637. *
  1638. */
  1639. static int mpage_da_map_blocks(struct mpage_da_data *mpd)
  1640. {
  1641. int err = 0;
  1642. struct buffer_head new;
  1643. struct buffer_head *lbh = &mpd->lbh;
  1644. sector_t next = lbh->b_blocknr;
  1645. /*
  1646. * We consider only non-mapped and non-allocated blocks
  1647. */
  1648. if (buffer_mapped(lbh) && !buffer_delay(lbh))
  1649. return 0;
  1650. new.b_state = lbh->b_state;
  1651. new.b_blocknr = 0;
  1652. new.b_size = lbh->b_size;
  1653. /*
  1654. * If we didn't accumulate anything
  1655. * to write simply return
  1656. */
  1657. if (!new.b_size)
  1658. return 0;
  1659. err = mpd->get_block(mpd->inode, next, &new, 1);
  1660. if (err) {
  1661. /* If get block returns with error
  1662. * we simply return. Later writepage
  1663. * will redirty the page and writepages
  1664. * will find the dirty page again
  1665. */
  1666. if (err == -EAGAIN)
  1667. return 0;
  1668. /*
  1669. * get block failure will cause us
  1670. * to loop in writepages. Because
  1671. * a_ops->writepage won't be able to
  1672. * make progress. The page will be redirtied
  1673. * by writepage and writepages will again
  1674. * try to write the same.
  1675. */
  1676. printk(KERN_EMERG "%s block allocation failed for inode %lu "
  1677. "at logical offset %llu with max blocks "
  1678. "%zd with error %d\n",
  1679. __func__, mpd->inode->i_ino,
  1680. (unsigned long long)next,
  1681. lbh->b_size >> mpd->inode->i_blkbits, err);
  1682. printk(KERN_EMERG "This should not happen.!! "
  1683. "Data will be lost\n");
  1684. if (err == -ENOSPC) {
  1685. printk(KERN_CRIT "Total free blocks count %lld\n",
  1686. ext4_count_free_blocks(mpd->inode->i_sb));
  1687. }
  1688. /* invlaidate all the pages */
  1689. ext4_da_block_invalidatepages(mpd, next,
  1690. lbh->b_size >> mpd->inode->i_blkbits);
  1691. return err;
  1692. }
  1693. BUG_ON(new.b_size == 0);
  1694. if (buffer_new(&new))
  1695. __unmap_underlying_blocks(mpd->inode, &new);
  1696. /*
  1697. * If blocks are delayed marked, we need to
  1698. * put actual blocknr and drop delayed bit
  1699. */
  1700. if (buffer_delay(lbh) || buffer_unwritten(lbh))
  1701. mpage_put_bnr_to_bhs(mpd, next, &new);
  1702. return 0;
  1703. }
  1704. #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
  1705. (1 << BH_Delay) | (1 << BH_Unwritten))
  1706. /*
  1707. * mpage_add_bh_to_extent - try to add one more block to extent of blocks
  1708. *
  1709. * @mpd->lbh - extent of blocks
  1710. * @logical - logical number of the block in the file
  1711. * @bh - bh of the block (used to access block's state)
  1712. *
  1713. * the function is used to collect contig. blocks in same state
  1714. */
  1715. static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
  1716. sector_t logical, struct buffer_head *bh)
  1717. {
  1718. sector_t next;
  1719. size_t b_size = bh->b_size;
  1720. struct buffer_head *lbh = &mpd->lbh;
  1721. int nrblocks = lbh->b_size >> mpd->inode->i_blkbits;
  1722. /* check if thereserved journal credits might overflow */
  1723. if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
  1724. if (nrblocks >= EXT4_MAX_TRANS_DATA) {
  1725. /*
  1726. * With non-extent format we are limited by the journal
  1727. * credit available. Total credit needed to insert
  1728. * nrblocks contiguous blocks is dependent on the
  1729. * nrblocks. So limit nrblocks.
  1730. */
  1731. goto flush_it;
  1732. } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
  1733. EXT4_MAX_TRANS_DATA) {
  1734. /*
  1735. * Adding the new buffer_head would make it cross the
  1736. * allowed limit for which we have journal credit
  1737. * reserved. So limit the new bh->b_size
  1738. */
  1739. b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
  1740. mpd->inode->i_blkbits;
  1741. /* we will do mpage_da_submit_io in the next loop */
  1742. }
  1743. }
  1744. /*
  1745. * First block in the extent
  1746. */
  1747. if (lbh->b_size == 0) {
  1748. lbh->b_blocknr = logical;
  1749. lbh->b_size = b_size;
  1750. lbh->b_state = bh->b_state & BH_FLAGS;
  1751. return;
  1752. }
  1753. next = lbh->b_blocknr + nrblocks;
  1754. /*
  1755. * Can we merge the block to our big extent?
  1756. */
  1757. if (logical == next && (bh->b_state & BH_FLAGS) == lbh->b_state) {
  1758. lbh->b_size += b_size;
  1759. return;
  1760. }
  1761. flush_it:
  1762. /*
  1763. * We couldn't merge the block to our extent, so we
  1764. * need to flush current extent and start new one
  1765. */
  1766. if (mpage_da_map_blocks(mpd) == 0)
  1767. mpage_da_submit_io(mpd);
  1768. mpd->io_done = 1;
  1769. return;
  1770. }
  1771. /*
  1772. * __mpage_da_writepage - finds extent of pages and blocks
  1773. *
  1774. * @page: page to consider
  1775. * @wbc: not used, we just follow rules
  1776. * @data: context
  1777. *
  1778. * The function finds extents of pages and scan them for all blocks.
  1779. */
  1780. static int __mpage_da_writepage(struct page *page,
  1781. struct writeback_control *wbc, void *data)
  1782. {
  1783. struct mpage_da_data *mpd = data;
  1784. struct inode *inode = mpd->inode;
  1785. struct buffer_head *bh, *head, fake;
  1786. sector_t logical;
  1787. if (mpd->io_done) {
  1788. /*
  1789. * Rest of the page in the page_vec
  1790. * redirty then and skip then. We will
  1791. * try to to write them again after
  1792. * starting a new transaction
  1793. */
  1794. redirty_page_for_writepage(wbc, page);
  1795. unlock_page(page);
  1796. return MPAGE_DA_EXTENT_TAIL;
  1797. }
  1798. /*
  1799. * Can we merge this page to current extent?
  1800. */
  1801. if (mpd->next_page != page->index) {
  1802. /*
  1803. * Nope, we can't. So, we map non-allocated blocks
  1804. * and start IO on them using writepage()
  1805. */
  1806. if (mpd->next_page != mpd->first_page) {
  1807. if (mpage_da_map_blocks(mpd) == 0)
  1808. mpage_da_submit_io(mpd);
  1809. /*
  1810. * skip rest of the page in the page_vec
  1811. */
  1812. mpd->io_done = 1;
  1813. redirty_page_for_writepage(wbc, page);
  1814. unlock_page(page);
  1815. return MPAGE_DA_EXTENT_TAIL;
  1816. }
  1817. /*
  1818. * Start next extent of pages ...
  1819. */
  1820. mpd->first_page = page->index;
  1821. /*
  1822. * ... and blocks
  1823. */
  1824. mpd->lbh.b_size = 0;
  1825. mpd->lbh.b_state = 0;
  1826. mpd->lbh.b_blocknr = 0;
  1827. }
  1828. mpd->next_page = page->index + 1;
  1829. logical = (sector_t) page->index <<
  1830. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1831. if (!page_has_buffers(page)) {
  1832. /*
  1833. * There is no attached buffer heads yet (mmap?)
  1834. * we treat the page asfull of dirty blocks
  1835. */
  1836. bh = &fake;
  1837. bh->b_size = PAGE_CACHE_SIZE;
  1838. bh->b_state = 0;
  1839. set_buffer_dirty(bh);
  1840. set_buffer_uptodate(bh);
  1841. mpage_add_bh_to_extent(mpd, logical, bh);
  1842. if (mpd->io_done)
  1843. return MPAGE_DA_EXTENT_TAIL;
  1844. } else {
  1845. /*
  1846. * Page with regular buffer heads, just add all dirty ones
  1847. */
  1848. head = page_buffers(page);
  1849. bh = head;
  1850. do {
  1851. BUG_ON(buffer_locked(bh));
  1852. if (buffer_dirty(bh) &&
  1853. (!buffer_mapped(bh) || buffer_delay(bh))) {
  1854. mpage_add_bh_to_extent(mpd, logical, bh);
  1855. if (mpd->io_done)
  1856. return MPAGE_DA_EXTENT_TAIL;
  1857. }
  1858. logical++;
  1859. } while ((bh = bh->b_this_page) != head);
  1860. }
  1861. return 0;
  1862. }
  1863. /*
  1864. * mpage_da_writepages - walk the list of dirty pages of the given
  1865. * address space, allocates non-allocated blocks, maps newly-allocated
  1866. * blocks to existing bhs and issue IO them
  1867. *
  1868. * @mapping: address space structure to write
  1869. * @wbc: subtract the number of written pages from *@wbc->nr_to_write
  1870. * @get_block: the filesystem's block mapper function.
  1871. *
  1872. * This is a library function, which implements the writepages()
  1873. * address_space_operation.
  1874. */
  1875. static int mpage_da_writepages(struct address_space *mapping,
  1876. struct writeback_control *wbc,
  1877. get_block_t get_block)
  1878. {
  1879. struct mpage_da_data mpd;
  1880. long to_write;
  1881. int ret;
  1882. if (!get_block)
  1883. return generic_writepages(mapping, wbc);
  1884. mpd.wbc = wbc;
  1885. mpd.inode = mapping->host;
  1886. mpd.lbh.b_size = 0;
  1887. mpd.lbh.b_state = 0;
  1888. mpd.lbh.b_blocknr = 0;
  1889. mpd.first_page = 0;
  1890. mpd.next_page = 0;
  1891. mpd.get_block = get_block;
  1892. mpd.io_done = 0;
  1893. mpd.pages_written = 0;
  1894. to_write = wbc->nr_to_write;
  1895. ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, &mpd);
  1896. /*
  1897. * Handle last extent of pages
  1898. */
  1899. if (!mpd.io_done && mpd.next_page != mpd.first_page) {
  1900. if (mpage_da_map_blocks(&mpd) == 0)
  1901. mpage_da_submit_io(&mpd);
  1902. }
  1903. wbc->nr_to_write = to_write - mpd.pages_written;
  1904. return ret;
  1905. }
  1906. /*
  1907. * this is a special callback for ->write_begin() only
  1908. * it's intention is to return mapped block or reserve space
  1909. */
  1910. static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1911. struct buffer_head *bh_result, int create)
  1912. {
  1913. int ret = 0;
  1914. BUG_ON(create == 0);
  1915. BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
  1916. /*
  1917. * first, we need to know whether the block is allocated already
  1918. * preallocated blocks are unmapped but should treated
  1919. * the same as allocated blocks.
  1920. */
  1921. ret = ext4_get_blocks_wrap(NULL, inode, iblock, 1, bh_result, 0, 0, 0);
  1922. if ((ret == 0) && !buffer_delay(bh_result)) {
  1923. /* the block isn't (pre)allocated yet, let's reserve space */
  1924. /*
  1925. * XXX: __block_prepare_write() unmaps passed block,
  1926. * is it OK?
  1927. */
  1928. ret = ext4_da_reserve_space(inode, 1);
  1929. if (ret)
  1930. /* not enough space to reserve */
  1931. return ret;
  1932. map_bh(bh_result, inode->i_sb, 0);
  1933. set_buffer_new(bh_result);
  1934. set_buffer_delay(bh_result);
  1935. } else if (ret > 0) {
  1936. bh_result->b_size = (ret << inode->i_blkbits);
  1937. ret = 0;
  1938. }
  1939. return ret;
  1940. }
  1941. #define EXT4_DELALLOC_RSVED 1
  1942. static int ext4_da_get_block_write(struct inode *inode, sector_t iblock,
  1943. struct buffer_head *bh_result, int create)
  1944. {
  1945. int ret;
  1946. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  1947. loff_t disksize = EXT4_I(inode)->i_disksize;
  1948. handle_t *handle = NULL;
  1949. handle = ext4_journal_current_handle();
  1950. if (!handle) {
  1951. ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
  1952. bh_result, 0, 0, 0);
  1953. BUG_ON(!ret);
  1954. } else {
  1955. ret = ext4_get_blocks_wrap(handle, inode, iblock, max_blocks,
  1956. bh_result, create, 0, EXT4_DELALLOC_RSVED);
  1957. }
  1958. if (ret > 0) {
  1959. bh_result->b_size = (ret << inode->i_blkbits);
  1960. /*
  1961. * Update on-disk size along with block allocation
  1962. * we don't use 'extend_disksize' as size may change
  1963. * within already allocated block -bzzz
  1964. */
  1965. disksize = ((loff_t) iblock + ret) << inode->i_blkbits;
  1966. if (disksize > i_size_read(inode))
  1967. disksize = i_size_read(inode);
  1968. if (disksize > EXT4_I(inode)->i_disksize) {
  1969. /*
  1970. * XXX: replace with spinlock if seen contended -bzzz
  1971. */
  1972. down_write(&EXT4_I(inode)->i_data_sem);
  1973. if (disksize > EXT4_I(inode)->i_disksize)
  1974. EXT4_I(inode)->i_disksize = disksize;
  1975. up_write(&EXT4_I(inode)->i_data_sem);
  1976. if (EXT4_I(inode)->i_disksize == disksize) {
  1977. ret = ext4_mark_inode_dirty(handle, inode);
  1978. return ret;
  1979. }
  1980. }
  1981. ret = 0;
  1982. }
  1983. return ret;
  1984. }
  1985. static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh)
  1986. {
  1987. /*
  1988. * unmapped buffer is possible for holes.
  1989. * delay buffer is possible with delayed allocation
  1990. */
  1991. return ((!buffer_mapped(bh) || buffer_delay(bh)) && buffer_dirty(bh));
  1992. }
  1993. static int ext4_normal_get_block_write(struct inode *inode, sector_t iblock,
  1994. struct buffer_head *bh_result, int create)
  1995. {
  1996. int ret = 0;
  1997. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  1998. /*
  1999. * we don't want to do block allocation in writepage
  2000. * so call get_block_wrap with create = 0
  2001. */
  2002. ret = ext4_get_blocks_wrap(NULL, inode, iblock, max_blocks,
  2003. bh_result, 0, 0, 0);
  2004. if (ret > 0) {
  2005. bh_result->b_size = (ret << inode->i_blkbits);
  2006. ret = 0;
  2007. }
  2008. return ret;
  2009. }
  2010. /*
  2011. * get called vi ext4_da_writepages after taking page lock (have journal handle)
  2012. * get called via journal_submit_inode_data_buffers (no journal handle)
  2013. * get called via shrink_page_list via pdflush (no journal handle)
  2014. * or grab_page_cache when doing write_begin (have journal handle)
  2015. */
  2016. static int ext4_da_writepage(struct page *page,
  2017. struct writeback_control *wbc)
  2018. {
  2019. int ret = 0;
  2020. loff_t size;
  2021. unsigned long len;
  2022. struct buffer_head *page_bufs;
  2023. struct inode *inode = page->mapping->host;
  2024. size = i_size_read(inode);
  2025. if (page->index == size >> PAGE_CACHE_SHIFT)
  2026. len = size & ~PAGE_CACHE_MASK;
  2027. else
  2028. len = PAGE_CACHE_SIZE;
  2029. if (page_has_buffers(page)) {
  2030. page_bufs = page_buffers(page);
  2031. if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  2032. ext4_bh_unmapped_or_delay)) {
  2033. /*
  2034. * We don't want to do block allocation
  2035. * So redirty the page and return
  2036. * We may reach here when we do a journal commit
  2037. * via journal_submit_inode_data_buffers.
  2038. * If we don't have mapping block we just ignore
  2039. * them. We can also reach here via shrink_page_list
  2040. */
  2041. redirty_page_for_writepage(wbc, page);
  2042. unlock_page(page);
  2043. return 0;
  2044. }
  2045. } else {
  2046. /*
  2047. * The test for page_has_buffers() is subtle:
  2048. * We know the page is dirty but it lost buffers. That means
  2049. * that at some moment in time after write_begin()/write_end()
  2050. * has been called all buffers have been clean and thus they
  2051. * must have been written at least once. So they are all
  2052. * mapped and we can happily proceed with mapping them
  2053. * and writing the page.
  2054. *
  2055. * Try to initialize the buffer_heads and check whether
  2056. * all are mapped and non delay. We don't want to
  2057. * do block allocation here.
  2058. */
  2059. ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
  2060. ext4_normal_get_block_write);
  2061. if (!ret) {
  2062. page_bufs = page_buffers(page);
  2063. /* check whether all are mapped and non delay */
  2064. if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  2065. ext4_bh_unmapped_or_delay)) {
  2066. redirty_page_for_writepage(wbc, page);
  2067. unlock_page(page);
  2068. return 0;
  2069. }
  2070. } else {
  2071. /*
  2072. * We can't do block allocation here
  2073. * so just redity the page and unlock
  2074. * and return
  2075. */
  2076. redirty_page_for_writepage(wbc, page);
  2077. unlock_page(page);
  2078. return 0;
  2079. }
  2080. }
  2081. if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
  2082. ret = nobh_writepage(page, ext4_normal_get_block_write, wbc);
  2083. else
  2084. ret = block_write_full_page(page,
  2085. ext4_normal_get_block_write,
  2086. wbc);
  2087. return ret;
  2088. }
  2089. /*
  2090. * This is called via ext4_da_writepages() to
  2091. * calulate the total number of credits to reserve to fit
  2092. * a single extent allocation into a single transaction,
  2093. * ext4_da_writpeages() will loop calling this before
  2094. * the block allocation.
  2095. */
  2096. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  2097. {
  2098. int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  2099. /*
  2100. * With non-extent format the journal credit needed to
  2101. * insert nrblocks contiguous block is dependent on
  2102. * number of contiguous block. So we will limit
  2103. * number of contiguous block to a sane value
  2104. */
  2105. if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
  2106. (max_blocks > EXT4_MAX_TRANS_DATA))
  2107. max_blocks = EXT4_MAX_TRANS_DATA;
  2108. return ext4_chunk_trans_blocks(inode, max_blocks);
  2109. }
  2110. static int ext4_da_writepages(struct address_space *mapping,
  2111. struct writeback_control *wbc)
  2112. {
  2113. handle_t *handle = NULL;
  2114. loff_t range_start = 0;
  2115. struct inode *inode = mapping->host;
  2116. int needed_blocks, ret = 0, nr_to_writebump = 0;
  2117. long to_write, pages_skipped = 0;
  2118. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  2119. /*
  2120. * No pages to write? This is mainly a kludge to avoid starting
  2121. * a transaction for special inodes like journal inode on last iput()
  2122. * because that could violate lock ordering on umount
  2123. */
  2124. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2125. return 0;
  2126. /*
  2127. * Make sure nr_to_write is >= sbi->s_mb_stream_request
  2128. * This make sure small files blocks are allocated in
  2129. * single attempt. This ensure that small files
  2130. * get less fragmented.
  2131. */
  2132. if (wbc->nr_to_write < sbi->s_mb_stream_request) {
  2133. nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
  2134. wbc->nr_to_write = sbi->s_mb_stream_request;
  2135. }
  2136. if (!wbc->range_cyclic)
  2137. /*
  2138. * If range_cyclic is not set force range_cont
  2139. * and save the old writeback_index
  2140. */
  2141. wbc->range_cont = 1;
  2142. range_start = wbc->range_start;
  2143. pages_skipped = wbc->pages_skipped;
  2144. restart_loop:
  2145. to_write = wbc->nr_to_write;
  2146. while (!ret && to_write > 0) {
  2147. /*
  2148. * we insert one extent at a time. So we need
  2149. * credit needed for single extent allocation.
  2150. * journalled mode is currently not supported
  2151. * by delalloc
  2152. */
  2153. BUG_ON(ext4_should_journal_data(inode));
  2154. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2155. /* start a new transaction*/
  2156. handle = ext4_journal_start(inode, needed_blocks);
  2157. if (IS_ERR(handle)) {
  2158. ret = PTR_ERR(handle);
  2159. printk(KERN_EMERG "%s: jbd2_start: "
  2160. "%ld pages, ino %lu; err %d\n", __func__,
  2161. wbc->nr_to_write, inode->i_ino, ret);
  2162. dump_stack();
  2163. goto out_writepages;
  2164. }
  2165. if (ext4_should_order_data(inode)) {
  2166. /*
  2167. * With ordered mode we need to add
  2168. * the inode to the journal handl
  2169. * when we do block allocation.
  2170. */
  2171. ret = ext4_jbd2_file_inode(handle, inode);
  2172. if (ret) {
  2173. ext4_journal_stop(handle);
  2174. goto out_writepages;
  2175. }
  2176. }
  2177. to_write -= wbc->nr_to_write;
  2178. ret = mpage_da_writepages(mapping, wbc,
  2179. ext4_da_get_block_write);
  2180. ext4_journal_stop(handle);
  2181. if (ret == MPAGE_DA_EXTENT_TAIL) {
  2182. /*
  2183. * got one extent now try with
  2184. * rest of the pages
  2185. */
  2186. to_write += wbc->nr_to_write;
  2187. ret = 0;
  2188. } else if (wbc->nr_to_write) {
  2189. /*
  2190. * There is no more writeout needed
  2191. * or we requested for a noblocking writeout
  2192. * and we found the device congested
  2193. */
  2194. to_write += wbc->nr_to_write;
  2195. break;
  2196. }
  2197. wbc->nr_to_write = to_write;
  2198. }
  2199. if (wbc->range_cont && (pages_skipped != wbc->pages_skipped)) {
  2200. /* We skipped pages in this loop */
  2201. wbc->range_start = range_start;
  2202. wbc->nr_to_write = to_write +
  2203. wbc->pages_skipped - pages_skipped;
  2204. wbc->pages_skipped = pages_skipped;
  2205. goto restart_loop;
  2206. }
  2207. out_writepages:
  2208. wbc->nr_to_write = to_write - nr_to_writebump;
  2209. wbc->range_start = range_start;
  2210. return ret;
  2211. }
  2212. #define FALL_BACK_TO_NONDELALLOC 1
  2213. static int ext4_nonda_switch(struct super_block *sb)
  2214. {
  2215. s64 free_blocks, dirty_blocks;
  2216. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2217. /*
  2218. * switch to non delalloc mode if we are running low
  2219. * on free block. The free block accounting via percpu
  2220. * counters can get slightly wrong with FBC_BATCH getting
  2221. * accumulated on each CPU without updating global counters
  2222. * Delalloc need an accurate free block accounting. So switch
  2223. * to non delalloc when we are near to error range.
  2224. */
  2225. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  2226. dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
  2227. if (2 * free_blocks < 3 * dirty_blocks ||
  2228. free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
  2229. /*
  2230. * free block count is less that 150% of dirty blocks
  2231. * or free blocks is less that watermark
  2232. */
  2233. return 1;
  2234. }
  2235. return 0;
  2236. }
  2237. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2238. loff_t pos, unsigned len, unsigned flags,
  2239. struct page **pagep, void **fsdata)
  2240. {
  2241. int ret, retries = 0;
  2242. struct page *page;
  2243. pgoff_t index;
  2244. unsigned from, to;
  2245. struct inode *inode = mapping->host;
  2246. handle_t *handle;
  2247. index = pos >> PAGE_CACHE_SHIFT;
  2248. from = pos & (PAGE_CACHE_SIZE - 1);
  2249. to = from + len;
  2250. if (ext4_nonda_switch(inode->i_sb)) {
  2251. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2252. return ext4_write_begin(file, mapping, pos,
  2253. len, flags, pagep, fsdata);
  2254. }
  2255. *fsdata = (void *)0;
  2256. retry:
  2257. /*
  2258. * With delayed allocation, we don't log the i_disksize update
  2259. * if there is delayed block allocation. But we still need
  2260. * to journalling the i_disksize update if writes to the end
  2261. * of file which has an already mapped buffer.
  2262. */
  2263. handle = ext4_journal_start(inode, 1);
  2264. if (IS_ERR(handle)) {
  2265. ret = PTR_ERR(handle);
  2266. goto out;
  2267. }
  2268. page = __grab_cache_page(mapping, index);
  2269. if (!page) {
  2270. ext4_journal_stop(handle);
  2271. ret = -ENOMEM;
  2272. goto out;
  2273. }
  2274. *pagep = page;
  2275. ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  2276. ext4_da_get_block_prep);
  2277. if (ret < 0) {
  2278. unlock_page(page);
  2279. ext4_journal_stop(handle);
  2280. page_cache_release(page);
  2281. }
  2282. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2283. goto retry;
  2284. out:
  2285. return ret;
  2286. }
  2287. /*
  2288. * Check if we should update i_disksize
  2289. * when write to the end of file but not require block allocation
  2290. */
  2291. static int ext4_da_should_update_i_disksize(struct page *page,
  2292. unsigned long offset)
  2293. {
  2294. struct buffer_head *bh;
  2295. struct inode *inode = page->mapping->host;
  2296. unsigned int idx;
  2297. int i;
  2298. bh = page_buffers(page);
  2299. idx = offset >> inode->i_blkbits;
  2300. for (i = 0; i < idx; i++)
  2301. bh = bh->b_this_page;
  2302. if (!buffer_mapped(bh) || (buffer_delay(bh)))
  2303. return 0;
  2304. return 1;
  2305. }
  2306. static int ext4_da_write_end(struct file *file,
  2307. struct address_space *mapping,
  2308. loff_t pos, unsigned len, unsigned copied,
  2309. struct page *page, void *fsdata)
  2310. {
  2311. struct inode *inode = mapping->host;
  2312. int ret = 0, ret2;
  2313. handle_t *handle = ext4_journal_current_handle();
  2314. loff_t new_i_size;
  2315. unsigned long start, end;
  2316. int write_mode = (int)(unsigned long)fsdata;
  2317. if (write_mode == FALL_BACK_TO_NONDELALLOC) {
  2318. if (ext4_should_order_data(inode)) {
  2319. return ext4_ordered_write_end(file, mapping, pos,
  2320. len, copied, page, fsdata);
  2321. } else if (ext4_should_writeback_data(inode)) {
  2322. return ext4_writeback_write_end(file, mapping, pos,
  2323. len, copied, page, fsdata);
  2324. } else {
  2325. BUG();
  2326. }
  2327. }
  2328. start = pos & (PAGE_CACHE_SIZE - 1);
  2329. end = start + copied - 1;
  2330. /*
  2331. * generic_write_end() will run mark_inode_dirty() if i_size
  2332. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2333. * into that.
  2334. */
  2335. new_i_size = pos + copied;
  2336. if (new_i_size > EXT4_I(inode)->i_disksize) {
  2337. if (ext4_da_should_update_i_disksize(page, end)) {
  2338. down_write(&EXT4_I(inode)->i_data_sem);
  2339. if (new_i_size > EXT4_I(inode)->i_disksize) {
  2340. /*
  2341. * Updating i_disksize when extending file
  2342. * without needing block allocation
  2343. */
  2344. if (ext4_should_order_data(inode))
  2345. ret = ext4_jbd2_file_inode(handle,
  2346. inode);
  2347. EXT4_I(inode)->i_disksize = new_i_size;
  2348. }
  2349. up_write(&EXT4_I(inode)->i_data_sem);
  2350. }
  2351. }
  2352. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2353. page, fsdata);
  2354. copied = ret2;
  2355. if (ret2 < 0)
  2356. ret = ret2;
  2357. ret2 = ext4_journal_stop(handle);
  2358. if (!ret)
  2359. ret = ret2;
  2360. return ret ? ret : copied;
  2361. }
  2362. static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
  2363. {
  2364. /*
  2365. * Drop reserved blocks
  2366. */
  2367. BUG_ON(!PageLocked(page));
  2368. if (!page_has_buffers(page))
  2369. goto out;
  2370. ext4_da_page_release_reservation(page, offset);
  2371. out:
  2372. ext4_invalidatepage(page, offset);
  2373. return;
  2374. }
  2375. /*
  2376. * bmap() is special. It gets used by applications such as lilo and by
  2377. * the swapper to find the on-disk block of a specific piece of data.
  2378. *
  2379. * Naturally, this is dangerous if the block concerned is still in the
  2380. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2381. * filesystem and enables swap, then they may get a nasty shock when the
  2382. * data getting swapped to that swapfile suddenly gets overwritten by
  2383. * the original zero's written out previously to the journal and
  2384. * awaiting writeback in the kernel's buffer cache.
  2385. *
  2386. * So, if we see any bmap calls here on a modified, data-journaled file,
  2387. * take extra steps to flush any blocks which might be in the cache.
  2388. */
  2389. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2390. {
  2391. struct inode *inode = mapping->host;
  2392. journal_t *journal;
  2393. int err;
  2394. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2395. test_opt(inode->i_sb, DELALLOC)) {
  2396. /*
  2397. * With delalloc we want to sync the file
  2398. * so that we can make sure we allocate
  2399. * blocks for file
  2400. */
  2401. filemap_write_and_wait(mapping);
  2402. }
  2403. if (EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
  2404. /*
  2405. * This is a REALLY heavyweight approach, but the use of
  2406. * bmap on dirty files is expected to be extremely rare:
  2407. * only if we run lilo or swapon on a freshly made file
  2408. * do we expect this to happen.
  2409. *
  2410. * (bmap requires CAP_SYS_RAWIO so this does not
  2411. * represent an unprivileged user DOS attack --- we'd be
  2412. * in trouble if mortal users could trigger this path at
  2413. * will.)
  2414. *
  2415. * NB. EXT4_STATE_JDATA is not set on files other than
  2416. * regular files. If somebody wants to bmap a directory
  2417. * or symlink and gets confused because the buffer
  2418. * hasn't yet been flushed to disk, they deserve
  2419. * everything they get.
  2420. */
  2421. EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
  2422. journal = EXT4_JOURNAL(inode);
  2423. jbd2_journal_lock_updates(journal);
  2424. err = jbd2_journal_flush(journal);
  2425. jbd2_journal_unlock_updates(journal);
  2426. if (err)
  2427. return 0;
  2428. }
  2429. return generic_block_bmap(mapping, block, ext4_get_block);
  2430. }
  2431. static int bget_one(handle_t *handle, struct buffer_head *bh)
  2432. {
  2433. get_bh(bh);
  2434. return 0;
  2435. }
  2436. static int bput_one(handle_t *handle, struct buffer_head *bh)
  2437. {
  2438. put_bh(bh);
  2439. return 0;
  2440. }
  2441. /*
  2442. * Note that we don't need to start a transaction unless we're journaling data
  2443. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  2444. * need to file the inode to the transaction's list in ordered mode because if
  2445. * we are writing back data added by write(), the inode is already there and if
  2446. * we are writing back data modified via mmap(), noone guarantees in which
  2447. * transaction the data will hit the disk. In case we are journaling data, we
  2448. * cannot start transaction directly because transaction start ranks above page
  2449. * lock so we have to do some magic.
  2450. *
  2451. * In all journaling modes block_write_full_page() will start the I/O.
  2452. *
  2453. * Problem:
  2454. *
  2455. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  2456. * ext4_writepage()
  2457. *
  2458. * Similar for:
  2459. *
  2460. * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ...
  2461. *
  2462. * Same applies to ext4_get_block(). We will deadlock on various things like
  2463. * lock_journal and i_data_sem
  2464. *
  2465. * Setting PF_MEMALLOC here doesn't work - too many internal memory
  2466. * allocations fail.
  2467. *
  2468. * 16May01: If we're reentered then journal_current_handle() will be
  2469. * non-zero. We simply *return*.
  2470. *
  2471. * 1 July 2001: @@@ FIXME:
  2472. * In journalled data mode, a data buffer may be metadata against the
  2473. * current transaction. But the same file is part of a shared mapping
  2474. * and someone does a writepage() on it.
  2475. *
  2476. * We will move the buffer onto the async_data list, but *after* it has
  2477. * been dirtied. So there's a small window where we have dirty data on
  2478. * BJ_Metadata.
  2479. *
  2480. * Note that this only applies to the last partial page in the file. The
  2481. * bit which block_write_full_page() uses prepare/commit for. (That's
  2482. * broken code anyway: it's wrong for msync()).
  2483. *
  2484. * It's a rare case: affects the final partial page, for journalled data
  2485. * where the file is subject to bith write() and writepage() in the same
  2486. * transction. To fix it we'll need a custom block_write_full_page().
  2487. * We'll probably need that anyway for journalling writepage() output.
  2488. *
  2489. * We don't honour synchronous mounts for writepage(). That would be
  2490. * disastrous. Any write() or metadata operation will sync the fs for
  2491. * us.
  2492. *
  2493. */
  2494. static int __ext4_normal_writepage(struct page *page,
  2495. struct writeback_control *wbc)
  2496. {
  2497. struct inode *inode = page->mapping->host;
  2498. if (test_opt(inode->i_sb, NOBH))
  2499. return nobh_writepage(page,
  2500. ext4_normal_get_block_write, wbc);
  2501. else
  2502. return block_write_full_page(page,
  2503. ext4_normal_get_block_write,
  2504. wbc);
  2505. }
  2506. static int ext4_normal_writepage(struct page *page,
  2507. struct writeback_control *wbc)
  2508. {
  2509. struct inode *inode = page->mapping->host;
  2510. loff_t size = i_size_read(inode);
  2511. loff_t len;
  2512. J_ASSERT(PageLocked(page));
  2513. if (page->index == size >> PAGE_CACHE_SHIFT)
  2514. len = size & ~PAGE_CACHE_MASK;
  2515. else
  2516. len = PAGE_CACHE_SIZE;
  2517. if (page_has_buffers(page)) {
  2518. /* if page has buffers it should all be mapped
  2519. * and allocated. If there are not buffers attached
  2520. * to the page we know the page is dirty but it lost
  2521. * buffers. That means that at some moment in time
  2522. * after write_begin() / write_end() has been called
  2523. * all buffers have been clean and thus they must have been
  2524. * written at least once. So they are all mapped and we can
  2525. * happily proceed with mapping them and writing the page.
  2526. */
  2527. BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
  2528. ext4_bh_unmapped_or_delay));
  2529. }
  2530. if (!ext4_journal_current_handle())
  2531. return __ext4_normal_writepage(page, wbc);
  2532. redirty_page_for_writepage(wbc, page);
  2533. unlock_page(page);
  2534. return 0;
  2535. }
  2536. static int __ext4_journalled_writepage(struct page *page,
  2537. struct writeback_control *wbc)
  2538. {
  2539. struct address_space *mapping = page->mapping;
  2540. struct inode *inode = mapping->host;
  2541. struct buffer_head *page_bufs;
  2542. handle_t *handle = NULL;
  2543. int ret = 0;
  2544. int err;
  2545. ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
  2546. ext4_normal_get_block_write);
  2547. if (ret != 0)
  2548. goto out_unlock;
  2549. page_bufs = page_buffers(page);
  2550. walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL,
  2551. bget_one);
  2552. /* As soon as we unlock the page, it can go away, but we have
  2553. * references to buffers so we are safe */
  2554. unlock_page(page);
  2555. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  2556. if (IS_ERR(handle)) {
  2557. ret = PTR_ERR(handle);
  2558. goto out;
  2559. }
  2560. ret = walk_page_buffers(handle, page_bufs, 0,
  2561. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
  2562. err = walk_page_buffers(handle, page_bufs, 0,
  2563. PAGE_CACHE_SIZE, NULL, write_end_fn);
  2564. if (ret == 0)
  2565. ret = err;
  2566. err = ext4_journal_stop(handle);
  2567. if (!ret)
  2568. ret = err;
  2569. walk_page_buffers(handle, page_bufs, 0,
  2570. PAGE_CACHE_SIZE, NULL, bput_one);
  2571. EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
  2572. goto out;
  2573. out_unlock:
  2574. unlock_page(page);
  2575. out:
  2576. return ret;
  2577. }
  2578. static int ext4_journalled_writepage(struct page *page,
  2579. struct writeback_control *wbc)
  2580. {
  2581. struct inode *inode = page->mapping->host;
  2582. loff_t size = i_size_read(inode);
  2583. loff_t len;
  2584. J_ASSERT(PageLocked(page));
  2585. if (page->index == size >> PAGE_CACHE_SHIFT)
  2586. len = size & ~PAGE_CACHE_MASK;
  2587. else
  2588. len = PAGE_CACHE_SIZE;
  2589. if (page_has_buffers(page)) {
  2590. /* if page has buffers it should all be mapped
  2591. * and allocated. If there are not buffers attached
  2592. * to the page we know the page is dirty but it lost
  2593. * buffers. That means that at some moment in time
  2594. * after write_begin() / write_end() has been called
  2595. * all buffers have been clean and thus they must have been
  2596. * written at least once. So they are all mapped and we can
  2597. * happily proceed with mapping them and writing the page.
  2598. */
  2599. BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
  2600. ext4_bh_unmapped_or_delay));
  2601. }
  2602. if (ext4_journal_current_handle())
  2603. goto no_write;
  2604. if (PageChecked(page)) {
  2605. /*
  2606. * It's mmapped pagecache. Add buffers and journal it. There
  2607. * doesn't seem much point in redirtying the page here.
  2608. */
  2609. ClearPageChecked(page);
  2610. return __ext4_journalled_writepage(page, wbc);
  2611. } else {
  2612. /*
  2613. * It may be a page full of checkpoint-mode buffers. We don't
  2614. * really know unless we go poke around in the buffer_heads.
  2615. * But block_write_full_page will do the right thing.
  2616. */
  2617. return block_write_full_page(page,
  2618. ext4_normal_get_block_write,
  2619. wbc);
  2620. }
  2621. no_write:
  2622. redirty_page_for_writepage(wbc, page);
  2623. unlock_page(page);
  2624. return 0;
  2625. }
  2626. static int ext4_readpage(struct file *file, struct page *page)
  2627. {
  2628. return mpage_readpage(page, ext4_get_block);
  2629. }
  2630. static int
  2631. ext4_readpages(struct file *file, struct address_space *mapping,
  2632. struct list_head *pages, unsigned nr_pages)
  2633. {
  2634. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2635. }
  2636. static void ext4_invalidatepage(struct page *page, unsigned long offset)
  2637. {
  2638. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2639. /*
  2640. * If it's a full truncate we just forget about the pending dirtying
  2641. */
  2642. if (offset == 0)
  2643. ClearPageChecked(page);
  2644. jbd2_journal_invalidatepage(journal, page, offset);
  2645. }
  2646. static int ext4_releasepage(struct page *page, gfp_t wait)
  2647. {
  2648. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2649. WARN_ON(PageChecked(page));
  2650. if (!page_has_buffers(page))
  2651. return 0;
  2652. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2653. }
  2654. /*
  2655. * If the O_DIRECT write will extend the file then add this inode to the
  2656. * orphan list. So recovery will truncate it back to the original size
  2657. * if the machine crashes during the write.
  2658. *
  2659. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  2660. * crashes then stale disk data _may_ be exposed inside the file. But current
  2661. * VFS code falls back into buffered path in that case so we are safe.
  2662. */
  2663. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  2664. const struct iovec *iov, loff_t offset,
  2665. unsigned long nr_segs)
  2666. {
  2667. struct file *file = iocb->ki_filp;
  2668. struct inode *inode = file->f_mapping->host;
  2669. struct ext4_inode_info *ei = EXT4_I(inode);
  2670. handle_t *handle;
  2671. ssize_t ret;
  2672. int orphan = 0;
  2673. size_t count = iov_length(iov, nr_segs);
  2674. if (rw == WRITE) {
  2675. loff_t final_size = offset + count;
  2676. if (final_size > inode->i_size) {
  2677. /* Credits for sb + inode write */
  2678. handle = ext4_journal_start(inode, 2);
  2679. if (IS_ERR(handle)) {
  2680. ret = PTR_ERR(handle);
  2681. goto out;
  2682. }
  2683. ret = ext4_orphan_add(handle, inode);
  2684. if (ret) {
  2685. ext4_journal_stop(handle);
  2686. goto out;
  2687. }
  2688. orphan = 1;
  2689. ei->i_disksize = inode->i_size;
  2690. ext4_journal_stop(handle);
  2691. }
  2692. }
  2693. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  2694. offset, nr_segs,
  2695. ext4_get_block, NULL);
  2696. if (orphan) {
  2697. int err;
  2698. /* Credits for sb + inode write */
  2699. handle = ext4_journal_start(inode, 2);
  2700. if (IS_ERR(handle)) {
  2701. /* This is really bad luck. We've written the data
  2702. * but cannot extend i_size. Bail out and pretend
  2703. * the write failed... */
  2704. ret = PTR_ERR(handle);
  2705. goto out;
  2706. }
  2707. if (inode->i_nlink)
  2708. ext4_orphan_del(handle, inode);
  2709. if (ret > 0) {
  2710. loff_t end = offset + ret;
  2711. if (end > inode->i_size) {
  2712. ei->i_disksize = end;
  2713. i_size_write(inode, end);
  2714. /*
  2715. * We're going to return a positive `ret'
  2716. * here due to non-zero-length I/O, so there's
  2717. * no way of reporting error returns from
  2718. * ext4_mark_inode_dirty() to userspace. So
  2719. * ignore it.
  2720. */
  2721. ext4_mark_inode_dirty(handle, inode);
  2722. }
  2723. }
  2724. err = ext4_journal_stop(handle);
  2725. if (ret == 0)
  2726. ret = err;
  2727. }
  2728. out:
  2729. return ret;
  2730. }
  2731. /*
  2732. * Pages can be marked dirty completely asynchronously from ext4's journalling
  2733. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  2734. * much here because ->set_page_dirty is called under VFS locks. The page is
  2735. * not necessarily locked.
  2736. *
  2737. * We cannot just dirty the page and leave attached buffers clean, because the
  2738. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  2739. * or jbddirty because all the journalling code will explode.
  2740. *
  2741. * So what we do is to mark the page "pending dirty" and next time writepage
  2742. * is called, propagate that into the buffers appropriately.
  2743. */
  2744. static int ext4_journalled_set_page_dirty(struct page *page)
  2745. {
  2746. SetPageChecked(page);
  2747. return __set_page_dirty_nobuffers(page);
  2748. }
  2749. static const struct address_space_operations ext4_ordered_aops = {
  2750. .readpage = ext4_readpage,
  2751. .readpages = ext4_readpages,
  2752. .writepage = ext4_normal_writepage,
  2753. .sync_page = block_sync_page,
  2754. .write_begin = ext4_write_begin,
  2755. .write_end = ext4_ordered_write_end,
  2756. .bmap = ext4_bmap,
  2757. .invalidatepage = ext4_invalidatepage,
  2758. .releasepage = ext4_releasepage,
  2759. .direct_IO = ext4_direct_IO,
  2760. .migratepage = buffer_migrate_page,
  2761. .is_partially_uptodate = block_is_partially_uptodate,
  2762. };
  2763. static const struct address_space_operations ext4_writeback_aops = {
  2764. .readpage = ext4_readpage,
  2765. .readpages = ext4_readpages,
  2766. .writepage = ext4_normal_writepage,
  2767. .sync_page = block_sync_page,
  2768. .write_begin = ext4_write_begin,
  2769. .write_end = ext4_writeback_write_end,
  2770. .bmap = ext4_bmap,
  2771. .invalidatepage = ext4_invalidatepage,
  2772. .releasepage = ext4_releasepage,
  2773. .direct_IO = ext4_direct_IO,
  2774. .migratepage = buffer_migrate_page,
  2775. .is_partially_uptodate = block_is_partially_uptodate,
  2776. };
  2777. static const struct address_space_operations ext4_journalled_aops = {
  2778. .readpage = ext4_readpage,
  2779. .readpages = ext4_readpages,
  2780. .writepage = ext4_journalled_writepage,
  2781. .sync_page = block_sync_page,
  2782. .write_begin = ext4_write_begin,
  2783. .write_end = ext4_journalled_write_end,
  2784. .set_page_dirty = ext4_journalled_set_page_dirty,
  2785. .bmap = ext4_bmap,
  2786. .invalidatepage = ext4_invalidatepage,
  2787. .releasepage = ext4_releasepage,
  2788. .is_partially_uptodate = block_is_partially_uptodate,
  2789. };
  2790. static const struct address_space_operations ext4_da_aops = {
  2791. .readpage = ext4_readpage,
  2792. .readpages = ext4_readpages,
  2793. .writepage = ext4_da_writepage,
  2794. .writepages = ext4_da_writepages,
  2795. .sync_page = block_sync_page,
  2796. .write_begin = ext4_da_write_begin,
  2797. .write_end = ext4_da_write_end,
  2798. .bmap = ext4_bmap,
  2799. .invalidatepage = ext4_da_invalidatepage,
  2800. .releasepage = ext4_releasepage,
  2801. .direct_IO = ext4_direct_IO,
  2802. .migratepage = buffer_migrate_page,
  2803. .is_partially_uptodate = block_is_partially_uptodate,
  2804. };
  2805. void ext4_set_aops(struct inode *inode)
  2806. {
  2807. if (ext4_should_order_data(inode) &&
  2808. test_opt(inode->i_sb, DELALLOC))
  2809. inode->i_mapping->a_ops = &ext4_da_aops;
  2810. else if (ext4_should_order_data(inode))
  2811. inode->i_mapping->a_ops = &ext4_ordered_aops;
  2812. else if (ext4_should_writeback_data(inode) &&
  2813. test_opt(inode->i_sb, DELALLOC))
  2814. inode->i_mapping->a_ops = &ext4_da_aops;
  2815. else if (ext4_should_writeback_data(inode))
  2816. inode->i_mapping->a_ops = &ext4_writeback_aops;
  2817. else
  2818. inode->i_mapping->a_ops = &ext4_journalled_aops;
  2819. }
  2820. /*
  2821. * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
  2822. * up to the end of the block which corresponds to `from'.
  2823. * This required during truncate. We need to physically zero the tail end
  2824. * of that block so it doesn't yield old data if the file is later grown.
  2825. */
  2826. int ext4_block_truncate_page(handle_t *handle,
  2827. struct address_space *mapping, loff_t from)
  2828. {
  2829. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  2830. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2831. unsigned blocksize, length, pos;
  2832. ext4_lblk_t iblock;
  2833. struct inode *inode = mapping->host;
  2834. struct buffer_head *bh;
  2835. struct page *page;
  2836. int err = 0;
  2837. page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT);
  2838. if (!page)
  2839. return -EINVAL;
  2840. blocksize = inode->i_sb->s_blocksize;
  2841. length = blocksize - (offset & (blocksize - 1));
  2842. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  2843. /*
  2844. * For "nobh" option, we can only work if we don't need to
  2845. * read-in the page - otherwise we create buffers to do the IO.
  2846. */
  2847. if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
  2848. ext4_should_writeback_data(inode) && PageUptodate(page)) {
  2849. zero_user(page, offset, length);
  2850. set_page_dirty(page);
  2851. goto unlock;
  2852. }
  2853. if (!page_has_buffers(page))
  2854. create_empty_buffers(page, blocksize, 0);
  2855. /* Find the buffer that contains "offset" */
  2856. bh = page_buffers(page);
  2857. pos = blocksize;
  2858. while (offset >= pos) {
  2859. bh = bh->b_this_page;
  2860. iblock++;
  2861. pos += blocksize;
  2862. }
  2863. err = 0;
  2864. if (buffer_freed(bh)) {
  2865. BUFFER_TRACE(bh, "freed: skip");
  2866. goto unlock;
  2867. }
  2868. if (!buffer_mapped(bh)) {
  2869. BUFFER_TRACE(bh, "unmapped");
  2870. ext4_get_block(inode, iblock, bh, 0);
  2871. /* unmapped? It's a hole - nothing to do */
  2872. if (!buffer_mapped(bh)) {
  2873. BUFFER_TRACE(bh, "still unmapped");
  2874. goto unlock;
  2875. }
  2876. }
  2877. /* Ok, it's mapped. Make sure it's up-to-date */
  2878. if (PageUptodate(page))
  2879. set_buffer_uptodate(bh);
  2880. if (!buffer_uptodate(bh)) {
  2881. err = -EIO;
  2882. ll_rw_block(READ, 1, &bh);
  2883. wait_on_buffer(bh);
  2884. /* Uhhuh. Read error. Complain and punt. */
  2885. if (!buffer_uptodate(bh))
  2886. goto unlock;
  2887. }
  2888. if (ext4_should_journal_data(inode)) {
  2889. BUFFER_TRACE(bh, "get write access");
  2890. err = ext4_journal_get_write_access(handle, bh);
  2891. if (err)
  2892. goto unlock;
  2893. }
  2894. zero_user(page, offset, length);
  2895. BUFFER_TRACE(bh, "zeroed end of block");
  2896. err = 0;
  2897. if (ext4_should_journal_data(inode)) {
  2898. err = ext4_journal_dirty_metadata(handle, bh);
  2899. } else {
  2900. if (ext4_should_order_data(inode))
  2901. err = ext4_jbd2_file_inode(handle, inode);
  2902. mark_buffer_dirty(bh);
  2903. }
  2904. unlock:
  2905. unlock_page(page);
  2906. page_cache_release(page);
  2907. return err;
  2908. }
  2909. /*
  2910. * Probably it should be a library function... search for first non-zero word
  2911. * or memcmp with zero_page, whatever is better for particular architecture.
  2912. * Linus?
  2913. */
  2914. static inline int all_zeroes(__le32 *p, __le32 *q)
  2915. {
  2916. while (p < q)
  2917. if (*p++)
  2918. return 0;
  2919. return 1;
  2920. }
  2921. /**
  2922. * ext4_find_shared - find the indirect blocks for partial truncation.
  2923. * @inode: inode in question
  2924. * @depth: depth of the affected branch
  2925. * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
  2926. * @chain: place to store the pointers to partial indirect blocks
  2927. * @top: place to the (detached) top of branch
  2928. *
  2929. * This is a helper function used by ext4_truncate().
  2930. *
  2931. * When we do truncate() we may have to clean the ends of several
  2932. * indirect blocks but leave the blocks themselves alive. Block is
  2933. * partially truncated if some data below the new i_size is refered
  2934. * from it (and it is on the path to the first completely truncated
  2935. * data block, indeed). We have to free the top of that path along
  2936. * with everything to the right of the path. Since no allocation
  2937. * past the truncation point is possible until ext4_truncate()
  2938. * finishes, we may safely do the latter, but top of branch may
  2939. * require special attention - pageout below the truncation point
  2940. * might try to populate it.
  2941. *
  2942. * We atomically detach the top of branch from the tree, store the
  2943. * block number of its root in *@top, pointers to buffer_heads of
  2944. * partially truncated blocks - in @chain[].bh and pointers to
  2945. * their last elements that should not be removed - in
  2946. * @chain[].p. Return value is the pointer to last filled element
  2947. * of @chain.
  2948. *
  2949. * The work left to caller to do the actual freeing of subtrees:
  2950. * a) free the subtree starting from *@top
  2951. * b) free the subtrees whose roots are stored in
  2952. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  2953. * c) free the subtrees growing from the inode past the @chain[0].
  2954. * (no partially truncated stuff there). */
  2955. static Indirect *ext4_find_shared(struct inode *inode, int depth,
  2956. ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top)
  2957. {
  2958. Indirect *partial, *p;
  2959. int k, err;
  2960. *top = 0;
  2961. /* Make k index the deepest non-null offest + 1 */
  2962. for (k = depth; k > 1 && !offsets[k-1]; k--)
  2963. ;
  2964. partial = ext4_get_branch(inode, k, offsets, chain, &err);
  2965. /* Writer: pointers */
  2966. if (!partial)
  2967. partial = chain + k-1;
  2968. /*
  2969. * If the branch acquired continuation since we've looked at it -
  2970. * fine, it should all survive and (new) top doesn't belong to us.
  2971. */
  2972. if (!partial->key && *partial->p)
  2973. /* Writer: end */
  2974. goto no_top;
  2975. for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
  2976. ;
  2977. /*
  2978. * OK, we've found the last block that must survive. The rest of our
  2979. * branch should be detached before unlocking. However, if that rest
  2980. * of branch is all ours and does not grow immediately from the inode
  2981. * it's easier to cheat and just decrement partial->p.
  2982. */
  2983. if (p == chain + k - 1 && p > chain) {
  2984. p->p--;
  2985. } else {
  2986. *top = *p->p;
  2987. /* Nope, don't do this in ext4. Must leave the tree intact */
  2988. #if 0
  2989. *p->p = 0;
  2990. #endif
  2991. }
  2992. /* Writer: end */
  2993. while (partial > p) {
  2994. brelse(partial->bh);
  2995. partial--;
  2996. }
  2997. no_top:
  2998. return partial;
  2999. }
  3000. /*
  3001. * Zero a number of block pointers in either an inode or an indirect block.
  3002. * If we restart the transaction we must again get write access to the
  3003. * indirect block for further modification.
  3004. *
  3005. * We release `count' blocks on disk, but (last - first) may be greater
  3006. * than `count' because there can be holes in there.
  3007. */
  3008. static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
  3009. struct buffer_head *bh, ext4_fsblk_t block_to_free,
  3010. unsigned long count, __le32 *first, __le32 *last)
  3011. {
  3012. __le32 *p;
  3013. if (try_to_extend_transaction(handle, inode)) {
  3014. if (bh) {
  3015. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  3016. ext4_journal_dirty_metadata(handle, bh);
  3017. }
  3018. ext4_mark_inode_dirty(handle, inode);
  3019. ext4_journal_test_restart(handle, inode);
  3020. if (bh) {
  3021. BUFFER_TRACE(bh, "retaking write access");
  3022. ext4_journal_get_write_access(handle, bh);
  3023. }
  3024. }
  3025. /*
  3026. * Any buffers which are on the journal will be in memory. We find
  3027. * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget()
  3028. * on them. We've already detached each block from the file, so
  3029. * bforget() in jbd2_journal_forget() should be safe.
  3030. *
  3031. * AKPM: turn on bforget in jbd2_journal_forget()!!!
  3032. */
  3033. for (p = first; p < last; p++) {
  3034. u32 nr = le32_to_cpu(*p);
  3035. if (nr) {
  3036. struct buffer_head *tbh;
  3037. *p = 0;
  3038. tbh = sb_find_get_block(inode->i_sb, nr);
  3039. ext4_forget(handle, 0, inode, tbh, nr);
  3040. }
  3041. }
  3042. ext4_free_blocks(handle, inode, block_to_free, count, 0);
  3043. }
  3044. /**
  3045. * ext4_free_data - free a list of data blocks
  3046. * @handle: handle for this transaction
  3047. * @inode: inode we are dealing with
  3048. * @this_bh: indirect buffer_head which contains *@first and *@last
  3049. * @first: array of block numbers
  3050. * @last: points immediately past the end of array
  3051. *
  3052. * We are freeing all blocks refered from that array (numbers are stored as
  3053. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  3054. *
  3055. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  3056. * blocks are contiguous then releasing them at one time will only affect one
  3057. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  3058. * actually use a lot of journal space.
  3059. *
  3060. * @this_bh will be %NULL if @first and @last point into the inode's direct
  3061. * block pointers.
  3062. */
  3063. static void ext4_free_data(handle_t *handle, struct inode *inode,
  3064. struct buffer_head *this_bh,
  3065. __le32 *first, __le32 *last)
  3066. {
  3067. ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
  3068. unsigned long count = 0; /* Number of blocks in the run */
  3069. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  3070. corresponding to
  3071. block_to_free */
  3072. ext4_fsblk_t nr; /* Current block # */
  3073. __le32 *p; /* Pointer into inode/ind
  3074. for current block */
  3075. int err;
  3076. if (this_bh) { /* For indirect block */
  3077. BUFFER_TRACE(this_bh, "get_write_access");
  3078. err = ext4_journal_get_write_access(handle, this_bh);
  3079. /* Important: if we can't update the indirect pointers
  3080. * to the blocks, we can't free them. */
  3081. if (err)
  3082. return;
  3083. }
  3084. for (p = first; p < last; p++) {
  3085. nr = le32_to_cpu(*p);
  3086. if (nr) {
  3087. /* accumulate blocks to free if they're contiguous */
  3088. if (count == 0) {
  3089. block_to_free = nr;
  3090. block_to_free_p = p;
  3091. count = 1;
  3092. } else if (nr == block_to_free + count) {
  3093. count++;
  3094. } else {
  3095. ext4_clear_blocks(handle, inode, this_bh,
  3096. block_to_free,
  3097. count, block_to_free_p, p);
  3098. block_to_free = nr;
  3099. block_to_free_p = p;
  3100. count = 1;
  3101. }
  3102. }
  3103. }
  3104. if (count > 0)
  3105. ext4_clear_blocks(handle, inode, this_bh, block_to_free,
  3106. count, block_to_free_p, p);
  3107. if (this_bh) {
  3108. BUFFER_TRACE(this_bh, "call ext4_journal_dirty_metadata");
  3109. /*
  3110. * The buffer head should have an attached journal head at this
  3111. * point. However, if the data is corrupted and an indirect
  3112. * block pointed to itself, it would have been detached when
  3113. * the block was cleared. Check for this instead of OOPSing.
  3114. */
  3115. if (bh2jh(this_bh))
  3116. ext4_journal_dirty_metadata(handle, this_bh);
  3117. else
  3118. ext4_error(inode->i_sb, __func__,
  3119. "circular indirect block detected, "
  3120. "inode=%lu, block=%llu",
  3121. inode->i_ino,
  3122. (unsigned long long) this_bh->b_blocknr);
  3123. }
  3124. }
  3125. /**
  3126. * ext4_free_branches - free an array of branches
  3127. * @handle: JBD handle for this transaction
  3128. * @inode: inode we are dealing with
  3129. * @parent_bh: the buffer_head which contains *@first and *@last
  3130. * @first: array of block numbers
  3131. * @last: pointer immediately past the end of array
  3132. * @depth: depth of the branches to free
  3133. *
  3134. * We are freeing all blocks refered from these branches (numbers are
  3135. * stored as little-endian 32-bit) and updating @inode->i_blocks
  3136. * appropriately.
  3137. */
  3138. static void ext4_free_branches(handle_t *handle, struct inode *inode,
  3139. struct buffer_head *parent_bh,
  3140. __le32 *first, __le32 *last, int depth)
  3141. {
  3142. ext4_fsblk_t nr;
  3143. __le32 *p;
  3144. if (is_handle_aborted(handle))
  3145. return;
  3146. if (depth--) {
  3147. struct buffer_head *bh;
  3148. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  3149. p = last;
  3150. while (--p >= first) {
  3151. nr = le32_to_cpu(*p);
  3152. if (!nr)
  3153. continue; /* A hole */
  3154. /* Go read the buffer for the next level down */
  3155. bh = sb_bread(inode->i_sb, nr);
  3156. /*
  3157. * A read failure? Report error and clear slot
  3158. * (should be rare).
  3159. */
  3160. if (!bh) {
  3161. ext4_error(inode->i_sb, "ext4_free_branches",
  3162. "Read failure, inode=%lu, block=%llu",
  3163. inode->i_ino, nr);
  3164. continue;
  3165. }
  3166. /* This zaps the entire block. Bottom up. */
  3167. BUFFER_TRACE(bh, "free child branches");
  3168. ext4_free_branches(handle, inode, bh,
  3169. (__le32 *) bh->b_data,
  3170. (__le32 *) bh->b_data + addr_per_block,
  3171. depth);
  3172. /*
  3173. * We've probably journalled the indirect block several
  3174. * times during the truncate. But it's no longer
  3175. * needed and we now drop it from the transaction via
  3176. * jbd2_journal_revoke().
  3177. *
  3178. * That's easy if it's exclusively part of this
  3179. * transaction. But if it's part of the committing
  3180. * transaction then jbd2_journal_forget() will simply
  3181. * brelse() it. That means that if the underlying
  3182. * block is reallocated in ext4_get_block(),
  3183. * unmap_underlying_metadata() will find this block
  3184. * and will try to get rid of it. damn, damn.
  3185. *
  3186. * If this block has already been committed to the
  3187. * journal, a revoke record will be written. And
  3188. * revoke records must be emitted *before* clearing
  3189. * this block's bit in the bitmaps.
  3190. */
  3191. ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
  3192. /*
  3193. * Everything below this this pointer has been
  3194. * released. Now let this top-of-subtree go.
  3195. *
  3196. * We want the freeing of this indirect block to be
  3197. * atomic in the journal with the updating of the
  3198. * bitmap block which owns it. So make some room in
  3199. * the journal.
  3200. *
  3201. * We zero the parent pointer *after* freeing its
  3202. * pointee in the bitmaps, so if extend_transaction()
  3203. * for some reason fails to put the bitmap changes and
  3204. * the release into the same transaction, recovery
  3205. * will merely complain about releasing a free block,
  3206. * rather than leaking blocks.
  3207. */
  3208. if (is_handle_aborted(handle))
  3209. return;
  3210. if (try_to_extend_transaction(handle, inode)) {
  3211. ext4_mark_inode_dirty(handle, inode);
  3212. ext4_journal_test_restart(handle, inode);
  3213. }
  3214. ext4_free_blocks(handle, inode, nr, 1, 1);
  3215. if (parent_bh) {
  3216. /*
  3217. * The block which we have just freed is
  3218. * pointed to by an indirect block: journal it
  3219. */
  3220. BUFFER_TRACE(parent_bh, "get_write_access");
  3221. if (!ext4_journal_get_write_access(handle,
  3222. parent_bh)){
  3223. *p = 0;
  3224. BUFFER_TRACE(parent_bh,
  3225. "call ext4_journal_dirty_metadata");
  3226. ext4_journal_dirty_metadata(handle,
  3227. parent_bh);
  3228. }
  3229. }
  3230. }
  3231. } else {
  3232. /* We have reached the bottom of the tree. */
  3233. BUFFER_TRACE(parent_bh, "free data blocks");
  3234. ext4_free_data(handle, inode, parent_bh, first, last);
  3235. }
  3236. }
  3237. int ext4_can_truncate(struct inode *inode)
  3238. {
  3239. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3240. return 0;
  3241. if (S_ISREG(inode->i_mode))
  3242. return 1;
  3243. if (S_ISDIR(inode->i_mode))
  3244. return 1;
  3245. if (S_ISLNK(inode->i_mode))
  3246. return !ext4_inode_is_fast_symlink(inode);
  3247. return 0;
  3248. }
  3249. /*
  3250. * ext4_truncate()
  3251. *
  3252. * We block out ext4_get_block() block instantiations across the entire
  3253. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3254. * simultaneously on behalf of the same inode.
  3255. *
  3256. * As we work through the truncate and commmit bits of it to the journal there
  3257. * is one core, guiding principle: the file's tree must always be consistent on
  3258. * disk. We must be able to restart the truncate after a crash.
  3259. *
  3260. * The file's tree may be transiently inconsistent in memory (although it
  3261. * probably isn't), but whenever we close off and commit a journal transaction,
  3262. * the contents of (the filesystem + the journal) must be consistent and
  3263. * restartable. It's pretty simple, really: bottom up, right to left (although
  3264. * left-to-right works OK too).
  3265. *
  3266. * Note that at recovery time, journal replay occurs *before* the restart of
  3267. * truncate against the orphan inode list.
  3268. *
  3269. * The committed inode has the new, desired i_size (which is the same as
  3270. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3271. * that this inode's truncate did not complete and it will again call
  3272. * ext4_truncate() to have another go. So there will be instantiated blocks
  3273. * to the right of the truncation point in a crashed ext4 filesystem. But
  3274. * that's fine - as long as they are linked from the inode, the post-crash
  3275. * ext4_truncate() run will find them and release them.
  3276. */
  3277. void ext4_truncate(struct inode *inode)
  3278. {
  3279. handle_t *handle;
  3280. struct ext4_inode_info *ei = EXT4_I(inode);
  3281. __le32 *i_data = ei->i_data;
  3282. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  3283. struct address_space *mapping = inode->i_mapping;
  3284. ext4_lblk_t offsets[4];
  3285. Indirect chain[4];
  3286. Indirect *partial;
  3287. __le32 nr = 0;
  3288. int n;
  3289. ext4_lblk_t last_block;
  3290. unsigned blocksize = inode->i_sb->s_blocksize;
  3291. if (!ext4_can_truncate(inode))
  3292. return;
  3293. if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
  3294. ext4_ext_truncate(inode);
  3295. return;
  3296. }
  3297. handle = start_transaction(inode);
  3298. if (IS_ERR(handle))
  3299. return; /* AKPM: return what? */
  3300. last_block = (inode->i_size + blocksize-1)
  3301. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  3302. if (inode->i_size & (blocksize - 1))
  3303. if (ext4_block_truncate_page(handle, mapping, inode->i_size))
  3304. goto out_stop;
  3305. n = ext4_block_to_path(inode, last_block, offsets, NULL);
  3306. if (n == 0)
  3307. goto out_stop; /* error */
  3308. /*
  3309. * OK. This truncate is going to happen. We add the inode to the
  3310. * orphan list, so that if this truncate spans multiple transactions,
  3311. * and we crash, we will resume the truncate when the filesystem
  3312. * recovers. It also marks the inode dirty, to catch the new size.
  3313. *
  3314. * Implication: the file must always be in a sane, consistent
  3315. * truncatable state while each transaction commits.
  3316. */
  3317. if (ext4_orphan_add(handle, inode))
  3318. goto out_stop;
  3319. /*
  3320. * From here we block out all ext4_get_block() callers who want to
  3321. * modify the block allocation tree.
  3322. */
  3323. down_write(&ei->i_data_sem);
  3324. ext4_discard_reservation(inode);
  3325. /*
  3326. * The orphan list entry will now protect us from any crash which
  3327. * occurs before the truncate completes, so it is now safe to propagate
  3328. * the new, shorter inode size (held for now in i_size) into the
  3329. * on-disk inode. We do this via i_disksize, which is the value which
  3330. * ext4 *really* writes onto the disk inode.
  3331. */
  3332. ei->i_disksize = inode->i_size;
  3333. if (n == 1) { /* direct blocks */
  3334. ext4_free_data(handle, inode, NULL, i_data+offsets[0],
  3335. i_data + EXT4_NDIR_BLOCKS);
  3336. goto do_indirects;
  3337. }
  3338. partial = ext4_find_shared(inode, n, offsets, chain, &nr);
  3339. /* Kill the top of shared branch (not detached) */
  3340. if (nr) {
  3341. if (partial == chain) {
  3342. /* Shared branch grows from the inode */
  3343. ext4_free_branches(handle, inode, NULL,
  3344. &nr, &nr+1, (chain+n-1) - partial);
  3345. *partial->p = 0;
  3346. /*
  3347. * We mark the inode dirty prior to restart,
  3348. * and prior to stop. No need for it here.
  3349. */
  3350. } else {
  3351. /* Shared branch grows from an indirect block */
  3352. BUFFER_TRACE(partial->bh, "get_write_access");
  3353. ext4_free_branches(handle, inode, partial->bh,
  3354. partial->p,
  3355. partial->p+1, (chain+n-1) - partial);
  3356. }
  3357. }
  3358. /* Clear the ends of indirect blocks on the shared branch */
  3359. while (partial > chain) {
  3360. ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
  3361. (__le32*)partial->bh->b_data+addr_per_block,
  3362. (chain+n-1) - partial);
  3363. BUFFER_TRACE(partial->bh, "call brelse");
  3364. brelse (partial->bh);
  3365. partial--;
  3366. }
  3367. do_indirects:
  3368. /* Kill the remaining (whole) subtrees */
  3369. switch (offsets[0]) {
  3370. default:
  3371. nr = i_data[EXT4_IND_BLOCK];
  3372. if (nr) {
  3373. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  3374. i_data[EXT4_IND_BLOCK] = 0;
  3375. }
  3376. case EXT4_IND_BLOCK:
  3377. nr = i_data[EXT4_DIND_BLOCK];
  3378. if (nr) {
  3379. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  3380. i_data[EXT4_DIND_BLOCK] = 0;
  3381. }
  3382. case EXT4_DIND_BLOCK:
  3383. nr = i_data[EXT4_TIND_BLOCK];
  3384. if (nr) {
  3385. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  3386. i_data[EXT4_TIND_BLOCK] = 0;
  3387. }
  3388. case EXT4_TIND_BLOCK:
  3389. ;
  3390. }
  3391. up_write(&ei->i_data_sem);
  3392. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3393. ext4_mark_inode_dirty(handle, inode);
  3394. /*
  3395. * In a multi-transaction truncate, we only make the final transaction
  3396. * synchronous
  3397. */
  3398. if (IS_SYNC(inode))
  3399. handle->h_sync = 1;
  3400. out_stop:
  3401. /*
  3402. * If this was a simple ftruncate(), and the file will remain alive
  3403. * then we need to clear up the orphan record which we created above.
  3404. * However, if this was a real unlink then we were called by
  3405. * ext4_delete_inode(), and we allow that function to clean up the
  3406. * orphan info for us.
  3407. */
  3408. if (inode->i_nlink)
  3409. ext4_orphan_del(handle, inode);
  3410. ext4_journal_stop(handle);
  3411. }
  3412. static ext4_fsblk_t ext4_get_inode_block(struct super_block *sb,
  3413. unsigned long ino, struct ext4_iloc *iloc)
  3414. {
  3415. ext4_group_t block_group;
  3416. unsigned long offset;
  3417. ext4_fsblk_t block;
  3418. struct ext4_group_desc *gdp;
  3419. if (!ext4_valid_inum(sb, ino)) {
  3420. /*
  3421. * This error is already checked for in namei.c unless we are
  3422. * looking at an NFS filehandle, in which case no error
  3423. * report is needed
  3424. */
  3425. return 0;
  3426. }
  3427. block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3428. gdp = ext4_get_group_desc(sb, block_group, NULL);
  3429. if (!gdp)
  3430. return 0;
  3431. /*
  3432. * Figure out the offset within the block group inode table
  3433. */
  3434. offset = ((ino - 1) % EXT4_INODES_PER_GROUP(sb)) *
  3435. EXT4_INODE_SIZE(sb);
  3436. block = ext4_inode_table(sb, gdp) +
  3437. (offset >> EXT4_BLOCK_SIZE_BITS(sb));
  3438. iloc->block_group = block_group;
  3439. iloc->offset = offset & (EXT4_BLOCK_SIZE(sb) - 1);
  3440. return block;
  3441. }
  3442. /*
  3443. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3444. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3445. * data in memory that is needed to recreate the on-disk version of this
  3446. * inode.
  3447. */
  3448. static int __ext4_get_inode_loc(struct inode *inode,
  3449. struct ext4_iloc *iloc, int in_mem)
  3450. {
  3451. ext4_fsblk_t block;
  3452. struct buffer_head *bh;
  3453. block = ext4_get_inode_block(inode->i_sb, inode->i_ino, iloc);
  3454. if (!block)
  3455. return -EIO;
  3456. bh = sb_getblk(inode->i_sb, block);
  3457. if (!bh) {
  3458. ext4_error (inode->i_sb, "ext4_get_inode_loc",
  3459. "unable to read inode block - "
  3460. "inode=%lu, block=%llu",
  3461. inode->i_ino, block);
  3462. return -EIO;
  3463. }
  3464. if (!buffer_uptodate(bh)) {
  3465. lock_buffer(bh);
  3466. /*
  3467. * If the buffer has the write error flag, we have failed
  3468. * to write out another inode in the same block. In this
  3469. * case, we don't have to read the block because we may
  3470. * read the old inode data successfully.
  3471. */
  3472. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3473. set_buffer_uptodate(bh);
  3474. if (buffer_uptodate(bh)) {
  3475. /* someone brought it uptodate while we waited */
  3476. unlock_buffer(bh);
  3477. goto has_buffer;
  3478. }
  3479. /*
  3480. * If we have all information of the inode in memory and this
  3481. * is the only valid inode in the block, we need not read the
  3482. * block.
  3483. */
  3484. if (in_mem) {
  3485. struct buffer_head *bitmap_bh;
  3486. struct ext4_group_desc *desc;
  3487. int inodes_per_buffer;
  3488. int inode_offset, i;
  3489. ext4_group_t block_group;
  3490. int start;
  3491. block_group = (inode->i_ino - 1) /
  3492. EXT4_INODES_PER_GROUP(inode->i_sb);
  3493. inodes_per_buffer = bh->b_size /
  3494. EXT4_INODE_SIZE(inode->i_sb);
  3495. inode_offset = ((inode->i_ino - 1) %
  3496. EXT4_INODES_PER_GROUP(inode->i_sb));
  3497. start = inode_offset & ~(inodes_per_buffer - 1);
  3498. /* Is the inode bitmap in cache? */
  3499. desc = ext4_get_group_desc(inode->i_sb,
  3500. block_group, NULL);
  3501. if (!desc)
  3502. goto make_io;
  3503. bitmap_bh = sb_getblk(inode->i_sb,
  3504. ext4_inode_bitmap(inode->i_sb, desc));
  3505. if (!bitmap_bh)
  3506. goto make_io;
  3507. /*
  3508. * If the inode bitmap isn't in cache then the
  3509. * optimisation may end up performing two reads instead
  3510. * of one, so skip it.
  3511. */
  3512. if (!buffer_uptodate(bitmap_bh)) {
  3513. brelse(bitmap_bh);
  3514. goto make_io;
  3515. }
  3516. for (i = start; i < start + inodes_per_buffer; i++) {
  3517. if (i == inode_offset)
  3518. continue;
  3519. if (ext4_test_bit(i, bitmap_bh->b_data))
  3520. break;
  3521. }
  3522. brelse(bitmap_bh);
  3523. if (i == start + inodes_per_buffer) {
  3524. /* all other inodes are free, so skip I/O */
  3525. memset(bh->b_data, 0, bh->b_size);
  3526. set_buffer_uptodate(bh);
  3527. unlock_buffer(bh);
  3528. goto has_buffer;
  3529. }
  3530. }
  3531. make_io:
  3532. /*
  3533. * There are other valid inodes in the buffer, this inode
  3534. * has in-inode xattrs, or we don't have this inode in memory.
  3535. * Read the block from disk.
  3536. */
  3537. get_bh(bh);
  3538. bh->b_end_io = end_buffer_read_sync;
  3539. submit_bh(READ_META, bh);
  3540. wait_on_buffer(bh);
  3541. if (!buffer_uptodate(bh)) {
  3542. ext4_error(inode->i_sb, "ext4_get_inode_loc",
  3543. "unable to read inode block - "
  3544. "inode=%lu, block=%llu",
  3545. inode->i_ino, block);
  3546. brelse(bh);
  3547. return -EIO;
  3548. }
  3549. }
  3550. has_buffer:
  3551. iloc->bh = bh;
  3552. return 0;
  3553. }
  3554. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3555. {
  3556. /* We have all inode data except xattrs in memory here. */
  3557. return __ext4_get_inode_loc(inode, iloc,
  3558. !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
  3559. }
  3560. void ext4_set_inode_flags(struct inode *inode)
  3561. {
  3562. unsigned int flags = EXT4_I(inode)->i_flags;
  3563. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  3564. if (flags & EXT4_SYNC_FL)
  3565. inode->i_flags |= S_SYNC;
  3566. if (flags & EXT4_APPEND_FL)
  3567. inode->i_flags |= S_APPEND;
  3568. if (flags & EXT4_IMMUTABLE_FL)
  3569. inode->i_flags |= S_IMMUTABLE;
  3570. if (flags & EXT4_NOATIME_FL)
  3571. inode->i_flags |= S_NOATIME;
  3572. if (flags & EXT4_DIRSYNC_FL)
  3573. inode->i_flags |= S_DIRSYNC;
  3574. }
  3575. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3576. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3577. {
  3578. unsigned int flags = ei->vfs_inode.i_flags;
  3579. ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3580. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
  3581. if (flags & S_SYNC)
  3582. ei->i_flags |= EXT4_SYNC_FL;
  3583. if (flags & S_APPEND)
  3584. ei->i_flags |= EXT4_APPEND_FL;
  3585. if (flags & S_IMMUTABLE)
  3586. ei->i_flags |= EXT4_IMMUTABLE_FL;
  3587. if (flags & S_NOATIME)
  3588. ei->i_flags |= EXT4_NOATIME_FL;
  3589. if (flags & S_DIRSYNC)
  3590. ei->i_flags |= EXT4_DIRSYNC_FL;
  3591. }
  3592. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3593. struct ext4_inode_info *ei)
  3594. {
  3595. blkcnt_t i_blocks ;
  3596. struct inode *inode = &(ei->vfs_inode);
  3597. struct super_block *sb = inode->i_sb;
  3598. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3599. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  3600. /* we are using combined 48 bit field */
  3601. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3602. le32_to_cpu(raw_inode->i_blocks_lo);
  3603. if (ei->i_flags & EXT4_HUGE_FILE_FL) {
  3604. /* i_blocks represent file system block size */
  3605. return i_blocks << (inode->i_blkbits - 9);
  3606. } else {
  3607. return i_blocks;
  3608. }
  3609. } else {
  3610. return le32_to_cpu(raw_inode->i_blocks_lo);
  3611. }
  3612. }
  3613. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3614. {
  3615. struct ext4_iloc iloc;
  3616. struct ext4_inode *raw_inode;
  3617. struct ext4_inode_info *ei;
  3618. struct buffer_head *bh;
  3619. struct inode *inode;
  3620. long ret;
  3621. int block;
  3622. inode = iget_locked(sb, ino);
  3623. if (!inode)
  3624. return ERR_PTR(-ENOMEM);
  3625. if (!(inode->i_state & I_NEW))
  3626. return inode;
  3627. ei = EXT4_I(inode);
  3628. #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
  3629. ei->i_acl = EXT4_ACL_NOT_CACHED;
  3630. ei->i_default_acl = EXT4_ACL_NOT_CACHED;
  3631. #endif
  3632. ei->i_block_alloc_info = NULL;
  3633. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3634. if (ret < 0)
  3635. goto bad_inode;
  3636. bh = iloc.bh;
  3637. raw_inode = ext4_raw_inode(&iloc);
  3638. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3639. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3640. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3641. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3642. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3643. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3644. }
  3645. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  3646. ei->i_state = 0;
  3647. ei->i_dir_start_lookup = 0;
  3648. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3649. /* We now have enough fields to check if the inode was active or not.
  3650. * This is needed because nfsd might try to access dead inodes
  3651. * the test is that same one that e2fsck uses
  3652. * NeilBrown 1999oct15
  3653. */
  3654. if (inode->i_nlink == 0) {
  3655. if (inode->i_mode == 0 ||
  3656. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  3657. /* this inode is deleted */
  3658. brelse(bh);
  3659. ret = -ESTALE;
  3660. goto bad_inode;
  3661. }
  3662. /* The only unlinked inodes we let through here have
  3663. * valid i_mode and are being read by the orphan
  3664. * recovery code: that's fine, we're about to complete
  3665. * the process of deleting those. */
  3666. }
  3667. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  3668. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  3669. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  3670. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  3671. cpu_to_le32(EXT4_OS_HURD)) {
  3672. ei->i_file_acl |=
  3673. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  3674. }
  3675. inode->i_size = ext4_isize(raw_inode);
  3676. ei->i_disksize = inode->i_size;
  3677. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  3678. ei->i_block_group = iloc.block_group;
  3679. /*
  3680. * NOTE! The in-memory inode i_data array is in little-endian order
  3681. * even on big-endian machines: we do NOT byteswap the block numbers!
  3682. */
  3683. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3684. ei->i_data[block] = raw_inode->i_block[block];
  3685. INIT_LIST_HEAD(&ei->i_orphan);
  3686. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3687. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3688. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3689. EXT4_INODE_SIZE(inode->i_sb)) {
  3690. brelse(bh);
  3691. ret = -EIO;
  3692. goto bad_inode;
  3693. }
  3694. if (ei->i_extra_isize == 0) {
  3695. /* The extra space is currently unused. Use it. */
  3696. ei->i_extra_isize = sizeof(struct ext4_inode) -
  3697. EXT4_GOOD_OLD_INODE_SIZE;
  3698. } else {
  3699. __le32 *magic = (void *)raw_inode +
  3700. EXT4_GOOD_OLD_INODE_SIZE +
  3701. ei->i_extra_isize;
  3702. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
  3703. ei->i_state |= EXT4_STATE_XATTR;
  3704. }
  3705. } else
  3706. ei->i_extra_isize = 0;
  3707. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  3708. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  3709. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  3710. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  3711. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  3712. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3713. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3714. inode->i_version |=
  3715. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  3716. }
  3717. if (S_ISREG(inode->i_mode)) {
  3718. inode->i_op = &ext4_file_inode_operations;
  3719. inode->i_fop = &ext4_file_operations;
  3720. ext4_set_aops(inode);
  3721. } else if (S_ISDIR(inode->i_mode)) {
  3722. inode->i_op = &ext4_dir_inode_operations;
  3723. inode->i_fop = &ext4_dir_operations;
  3724. } else if (S_ISLNK(inode->i_mode)) {
  3725. if (ext4_inode_is_fast_symlink(inode))
  3726. inode->i_op = &ext4_fast_symlink_inode_operations;
  3727. else {
  3728. inode->i_op = &ext4_symlink_inode_operations;
  3729. ext4_set_aops(inode);
  3730. }
  3731. } else {
  3732. inode->i_op = &ext4_special_inode_operations;
  3733. if (raw_inode->i_block[0])
  3734. init_special_inode(inode, inode->i_mode,
  3735. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  3736. else
  3737. init_special_inode(inode, inode->i_mode,
  3738. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  3739. }
  3740. brelse(iloc.bh);
  3741. ext4_set_inode_flags(inode);
  3742. unlock_new_inode(inode);
  3743. return inode;
  3744. bad_inode:
  3745. iget_failed(inode);
  3746. return ERR_PTR(ret);
  3747. }
  3748. static int ext4_inode_blocks_set(handle_t *handle,
  3749. struct ext4_inode *raw_inode,
  3750. struct ext4_inode_info *ei)
  3751. {
  3752. struct inode *inode = &(ei->vfs_inode);
  3753. u64 i_blocks = inode->i_blocks;
  3754. struct super_block *sb = inode->i_sb;
  3755. int err = 0;
  3756. if (i_blocks <= ~0U) {
  3757. /*
  3758. * i_blocks can be represnted in a 32 bit variable
  3759. * as multiple of 512 bytes
  3760. */
  3761. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3762. raw_inode->i_blocks_high = 0;
  3763. ei->i_flags &= ~EXT4_HUGE_FILE_FL;
  3764. } else if (i_blocks <= 0xffffffffffffULL) {
  3765. /*
  3766. * i_blocks can be represented in a 48 bit variable
  3767. * as multiple of 512 bytes
  3768. */
  3769. err = ext4_update_rocompat_feature(handle, sb,
  3770. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  3771. if (err)
  3772. goto err_out;
  3773. /* i_block is stored in the split 48 bit fields */
  3774. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3775. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3776. ei->i_flags &= ~EXT4_HUGE_FILE_FL;
  3777. } else {
  3778. /*
  3779. * i_blocks should be represented in a 48 bit variable
  3780. * as multiple of file system block size
  3781. */
  3782. err = ext4_update_rocompat_feature(handle, sb,
  3783. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  3784. if (err)
  3785. goto err_out;
  3786. ei->i_flags |= EXT4_HUGE_FILE_FL;
  3787. /* i_block is stored in file system block size */
  3788. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  3789. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3790. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3791. }
  3792. err_out:
  3793. return err;
  3794. }
  3795. /*
  3796. * Post the struct inode info into an on-disk inode location in the
  3797. * buffer-cache. This gobbles the caller's reference to the
  3798. * buffer_head in the inode location struct.
  3799. *
  3800. * The caller must have write access to iloc->bh.
  3801. */
  3802. static int ext4_do_update_inode(handle_t *handle,
  3803. struct inode *inode,
  3804. struct ext4_iloc *iloc)
  3805. {
  3806. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  3807. struct ext4_inode_info *ei = EXT4_I(inode);
  3808. struct buffer_head *bh = iloc->bh;
  3809. int err = 0, rc, block;
  3810. /* For fields not not tracking in the in-memory inode,
  3811. * initialise them to zero for new inodes. */
  3812. if (ei->i_state & EXT4_STATE_NEW)
  3813. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  3814. ext4_get_inode_flags(ei);
  3815. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  3816. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3817. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
  3818. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
  3819. /*
  3820. * Fix up interoperability with old kernels. Otherwise, old inodes get
  3821. * re-used with the upper 16 bits of the uid/gid intact
  3822. */
  3823. if (!ei->i_dtime) {
  3824. raw_inode->i_uid_high =
  3825. cpu_to_le16(high_16_bits(inode->i_uid));
  3826. raw_inode->i_gid_high =
  3827. cpu_to_le16(high_16_bits(inode->i_gid));
  3828. } else {
  3829. raw_inode->i_uid_high = 0;
  3830. raw_inode->i_gid_high = 0;
  3831. }
  3832. } else {
  3833. raw_inode->i_uid_low =
  3834. cpu_to_le16(fs_high2lowuid(inode->i_uid));
  3835. raw_inode->i_gid_low =
  3836. cpu_to_le16(fs_high2lowgid(inode->i_gid));
  3837. raw_inode->i_uid_high = 0;
  3838. raw_inode->i_gid_high = 0;
  3839. }
  3840. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  3841. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  3842. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  3843. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  3844. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  3845. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  3846. goto out_brelse;
  3847. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  3848. /* clear the migrate flag in the raw_inode */
  3849. raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
  3850. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  3851. cpu_to_le32(EXT4_OS_HURD))
  3852. raw_inode->i_file_acl_high =
  3853. cpu_to_le16(ei->i_file_acl >> 32);
  3854. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  3855. ext4_isize_set(raw_inode, ei->i_disksize);
  3856. if (ei->i_disksize > 0x7fffffffULL) {
  3857. struct super_block *sb = inode->i_sb;
  3858. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3859. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  3860. EXT4_SB(sb)->s_es->s_rev_level ==
  3861. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  3862. /* If this is the first large file
  3863. * created, add a flag to the superblock.
  3864. */
  3865. err = ext4_journal_get_write_access(handle,
  3866. EXT4_SB(sb)->s_sbh);
  3867. if (err)
  3868. goto out_brelse;
  3869. ext4_update_dynamic_rev(sb);
  3870. EXT4_SET_RO_COMPAT_FEATURE(sb,
  3871. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  3872. sb->s_dirt = 1;
  3873. handle->h_sync = 1;
  3874. err = ext4_journal_dirty_metadata(handle,
  3875. EXT4_SB(sb)->s_sbh);
  3876. }
  3877. }
  3878. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  3879. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  3880. if (old_valid_dev(inode->i_rdev)) {
  3881. raw_inode->i_block[0] =
  3882. cpu_to_le32(old_encode_dev(inode->i_rdev));
  3883. raw_inode->i_block[1] = 0;
  3884. } else {
  3885. raw_inode->i_block[0] = 0;
  3886. raw_inode->i_block[1] =
  3887. cpu_to_le32(new_encode_dev(inode->i_rdev));
  3888. raw_inode->i_block[2] = 0;
  3889. }
  3890. } else for (block = 0; block < EXT4_N_BLOCKS; block++)
  3891. raw_inode->i_block[block] = ei->i_data[block];
  3892. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  3893. if (ei->i_extra_isize) {
  3894. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3895. raw_inode->i_version_hi =
  3896. cpu_to_le32(inode->i_version >> 32);
  3897. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  3898. }
  3899. BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata");
  3900. rc = ext4_journal_dirty_metadata(handle, bh);
  3901. if (!err)
  3902. err = rc;
  3903. ei->i_state &= ~EXT4_STATE_NEW;
  3904. out_brelse:
  3905. brelse(bh);
  3906. ext4_std_error(inode->i_sb, err);
  3907. return err;
  3908. }
  3909. /*
  3910. * ext4_write_inode()
  3911. *
  3912. * We are called from a few places:
  3913. *
  3914. * - Within generic_file_write() for O_SYNC files.
  3915. * Here, there will be no transaction running. We wait for any running
  3916. * trasnaction to commit.
  3917. *
  3918. * - Within sys_sync(), kupdate and such.
  3919. * We wait on commit, if tol to.
  3920. *
  3921. * - Within prune_icache() (PF_MEMALLOC == true)
  3922. * Here we simply return. We can't afford to block kswapd on the
  3923. * journal commit.
  3924. *
  3925. * In all cases it is actually safe for us to return without doing anything,
  3926. * because the inode has been copied into a raw inode buffer in
  3927. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  3928. * knfsd.
  3929. *
  3930. * Note that we are absolutely dependent upon all inode dirtiers doing the
  3931. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  3932. * which we are interested.
  3933. *
  3934. * It would be a bug for them to not do this. The code:
  3935. *
  3936. * mark_inode_dirty(inode)
  3937. * stuff();
  3938. * inode->i_size = expr;
  3939. *
  3940. * is in error because a kswapd-driven write_inode() could occur while
  3941. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  3942. * will no longer be on the superblock's dirty inode list.
  3943. */
  3944. int ext4_write_inode(struct inode *inode, int wait)
  3945. {
  3946. if (current->flags & PF_MEMALLOC)
  3947. return 0;
  3948. if (ext4_journal_current_handle()) {
  3949. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  3950. dump_stack();
  3951. return -EIO;
  3952. }
  3953. if (!wait)
  3954. return 0;
  3955. return ext4_force_commit(inode->i_sb);
  3956. }
  3957. /*
  3958. * ext4_setattr()
  3959. *
  3960. * Called from notify_change.
  3961. *
  3962. * We want to trap VFS attempts to truncate the file as soon as
  3963. * possible. In particular, we want to make sure that when the VFS
  3964. * shrinks i_size, we put the inode on the orphan list and modify
  3965. * i_disksize immediately, so that during the subsequent flushing of
  3966. * dirty pages and freeing of disk blocks, we can guarantee that any
  3967. * commit will leave the blocks being flushed in an unused state on
  3968. * disk. (On recovery, the inode will get truncated and the blocks will
  3969. * be freed, so we have a strong guarantee that no future commit will
  3970. * leave these blocks visible to the user.)
  3971. *
  3972. * Another thing we have to assure is that if we are in ordered mode
  3973. * and inode is still attached to the committing transaction, we must
  3974. * we start writeout of all the dirty pages which are being truncated.
  3975. * This way we are sure that all the data written in the previous
  3976. * transaction are already on disk (truncate waits for pages under
  3977. * writeback).
  3978. *
  3979. * Called with inode->i_mutex down.
  3980. */
  3981. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  3982. {
  3983. struct inode *inode = dentry->d_inode;
  3984. int error, rc = 0;
  3985. const unsigned int ia_valid = attr->ia_valid;
  3986. error = inode_change_ok(inode, attr);
  3987. if (error)
  3988. return error;
  3989. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  3990. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  3991. handle_t *handle;
  3992. /* (user+group)*(old+new) structure, inode write (sb,
  3993. * inode block, ? - but truncate inode update has it) */
  3994. handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
  3995. EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
  3996. if (IS_ERR(handle)) {
  3997. error = PTR_ERR(handle);
  3998. goto err_out;
  3999. }
  4000. error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
  4001. if (error) {
  4002. ext4_journal_stop(handle);
  4003. return error;
  4004. }
  4005. /* Update corresponding info in inode so that everything is in
  4006. * one transaction */
  4007. if (attr->ia_valid & ATTR_UID)
  4008. inode->i_uid = attr->ia_uid;
  4009. if (attr->ia_valid & ATTR_GID)
  4010. inode->i_gid = attr->ia_gid;
  4011. error = ext4_mark_inode_dirty(handle, inode);
  4012. ext4_journal_stop(handle);
  4013. }
  4014. if (attr->ia_valid & ATTR_SIZE) {
  4015. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
  4016. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4017. if (attr->ia_size > sbi->s_bitmap_maxbytes) {
  4018. error = -EFBIG;
  4019. goto err_out;
  4020. }
  4021. }
  4022. }
  4023. if (S_ISREG(inode->i_mode) &&
  4024. attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
  4025. handle_t *handle;
  4026. handle = ext4_journal_start(inode, 3);
  4027. if (IS_ERR(handle)) {
  4028. error = PTR_ERR(handle);
  4029. goto err_out;
  4030. }
  4031. error = ext4_orphan_add(handle, inode);
  4032. EXT4_I(inode)->i_disksize = attr->ia_size;
  4033. rc = ext4_mark_inode_dirty(handle, inode);
  4034. if (!error)
  4035. error = rc;
  4036. ext4_journal_stop(handle);
  4037. if (ext4_should_order_data(inode)) {
  4038. error = ext4_begin_ordered_truncate(inode,
  4039. attr->ia_size);
  4040. if (error) {
  4041. /* Do as much error cleanup as possible */
  4042. handle = ext4_journal_start(inode, 3);
  4043. if (IS_ERR(handle)) {
  4044. ext4_orphan_del(NULL, inode);
  4045. goto err_out;
  4046. }
  4047. ext4_orphan_del(handle, inode);
  4048. ext4_journal_stop(handle);
  4049. goto err_out;
  4050. }
  4051. }
  4052. }
  4053. rc = inode_setattr(inode, attr);
  4054. /* If inode_setattr's call to ext4_truncate failed to get a
  4055. * transaction handle at all, we need to clean up the in-core
  4056. * orphan list manually. */
  4057. if (inode->i_nlink)
  4058. ext4_orphan_del(NULL, inode);
  4059. if (!rc && (ia_valid & ATTR_MODE))
  4060. rc = ext4_acl_chmod(inode);
  4061. err_out:
  4062. ext4_std_error(inode->i_sb, error);
  4063. if (!error)
  4064. error = rc;
  4065. return error;
  4066. }
  4067. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  4068. struct kstat *stat)
  4069. {
  4070. struct inode *inode;
  4071. unsigned long delalloc_blocks;
  4072. inode = dentry->d_inode;
  4073. generic_fillattr(inode, stat);
  4074. /*
  4075. * We can't update i_blocks if the block allocation is delayed
  4076. * otherwise in the case of system crash before the real block
  4077. * allocation is done, we will have i_blocks inconsistent with
  4078. * on-disk file blocks.
  4079. * We always keep i_blocks updated together with real
  4080. * allocation. But to not confuse with user, stat
  4081. * will return the blocks that include the delayed allocation
  4082. * blocks for this file.
  4083. */
  4084. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  4085. delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  4086. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  4087. stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
  4088. return 0;
  4089. }
  4090. static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
  4091. int chunk)
  4092. {
  4093. int indirects;
  4094. /* if nrblocks are contiguous */
  4095. if (chunk) {
  4096. /*
  4097. * With N contiguous data blocks, it need at most
  4098. * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
  4099. * 2 dindirect blocks
  4100. * 1 tindirect block
  4101. */
  4102. indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
  4103. return indirects + 3;
  4104. }
  4105. /*
  4106. * if nrblocks are not contiguous, worse case, each block touch
  4107. * a indirect block, and each indirect block touch a double indirect
  4108. * block, plus a triple indirect block
  4109. */
  4110. indirects = nrblocks * 2 + 1;
  4111. return indirects;
  4112. }
  4113. static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4114. {
  4115. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  4116. return ext4_indirect_trans_blocks(inode, nrblocks, 0);
  4117. return ext4_ext_index_trans_blocks(inode, nrblocks, 0);
  4118. }
  4119. /*
  4120. * Account for index blocks, block groups bitmaps and block group
  4121. * descriptor blocks if modify datablocks and index blocks
  4122. * worse case, the indexs blocks spread over different block groups
  4123. *
  4124. * If datablocks are discontiguous, they are possible to spread over
  4125. * different block groups too. If they are contiugous, with flexbg,
  4126. * they could still across block group boundary.
  4127. *
  4128. * Also account for superblock, inode, quota and xattr blocks
  4129. */
  4130. int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  4131. {
  4132. int groups, gdpblocks;
  4133. int idxblocks;
  4134. int ret = 0;
  4135. /*
  4136. * How many index blocks need to touch to modify nrblocks?
  4137. * The "Chunk" flag indicating whether the nrblocks is
  4138. * physically contiguous on disk
  4139. *
  4140. * For Direct IO and fallocate, they calls get_block to allocate
  4141. * one single extent at a time, so they could set the "Chunk" flag
  4142. */
  4143. idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
  4144. ret = idxblocks;
  4145. /*
  4146. * Now let's see how many group bitmaps and group descriptors need
  4147. * to account
  4148. */
  4149. groups = idxblocks;
  4150. if (chunk)
  4151. groups += 1;
  4152. else
  4153. groups += nrblocks;
  4154. gdpblocks = groups;
  4155. if (groups > EXT4_SB(inode->i_sb)->s_groups_count)
  4156. groups = EXT4_SB(inode->i_sb)->s_groups_count;
  4157. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  4158. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  4159. /* bitmaps and block group descriptor blocks */
  4160. ret += groups + gdpblocks;
  4161. /* Blocks for super block, inode, quota and xattr blocks */
  4162. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  4163. return ret;
  4164. }
  4165. /*
  4166. * Calulate the total number of credits to reserve to fit
  4167. * the modification of a single pages into a single transaction,
  4168. * which may include multiple chunks of block allocations.
  4169. *
  4170. * This could be called via ext4_write_begin()
  4171. *
  4172. * We need to consider the worse case, when
  4173. * one new block per extent.
  4174. */
  4175. int ext4_writepage_trans_blocks(struct inode *inode)
  4176. {
  4177. int bpp = ext4_journal_blocks_per_page(inode);
  4178. int ret;
  4179. ret = ext4_meta_trans_blocks(inode, bpp, 0);
  4180. /* Account for data blocks for journalled mode */
  4181. if (ext4_should_journal_data(inode))
  4182. ret += bpp;
  4183. return ret;
  4184. }
  4185. /*
  4186. * Calculate the journal credits for a chunk of data modification.
  4187. *
  4188. * This is called from DIO, fallocate or whoever calling
  4189. * ext4_get_blocks_wrap() to map/allocate a chunk of contigous disk blocks.
  4190. *
  4191. * journal buffers for data blocks are not included here, as DIO
  4192. * and fallocate do no need to journal data buffers.
  4193. */
  4194. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  4195. {
  4196. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  4197. }
  4198. /*
  4199. * The caller must have previously called ext4_reserve_inode_write().
  4200. * Give this, we know that the caller already has write access to iloc->bh.
  4201. */
  4202. int ext4_mark_iloc_dirty(handle_t *handle,
  4203. struct inode *inode, struct ext4_iloc *iloc)
  4204. {
  4205. int err = 0;
  4206. if (test_opt(inode->i_sb, I_VERSION))
  4207. inode_inc_iversion(inode);
  4208. /* the do_update_inode consumes one bh->b_count */
  4209. get_bh(iloc->bh);
  4210. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4211. err = ext4_do_update_inode(handle, inode, iloc);
  4212. put_bh(iloc->bh);
  4213. return err;
  4214. }
  4215. /*
  4216. * On success, We end up with an outstanding reference count against
  4217. * iloc->bh. This _must_ be cleaned up later.
  4218. */
  4219. int
  4220. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4221. struct ext4_iloc *iloc)
  4222. {
  4223. int err = 0;
  4224. if (handle) {
  4225. err = ext4_get_inode_loc(inode, iloc);
  4226. if (!err) {
  4227. BUFFER_TRACE(iloc->bh, "get_write_access");
  4228. err = ext4_journal_get_write_access(handle, iloc->bh);
  4229. if (err) {
  4230. brelse(iloc->bh);
  4231. iloc->bh = NULL;
  4232. }
  4233. }
  4234. }
  4235. ext4_std_error(inode->i_sb, err);
  4236. return err;
  4237. }
  4238. /*
  4239. * Expand an inode by new_extra_isize bytes.
  4240. * Returns 0 on success or negative error number on failure.
  4241. */
  4242. static int ext4_expand_extra_isize(struct inode *inode,
  4243. unsigned int new_extra_isize,
  4244. struct ext4_iloc iloc,
  4245. handle_t *handle)
  4246. {
  4247. struct ext4_inode *raw_inode;
  4248. struct ext4_xattr_ibody_header *header;
  4249. struct ext4_xattr_entry *entry;
  4250. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4251. return 0;
  4252. raw_inode = ext4_raw_inode(&iloc);
  4253. header = IHDR(inode, raw_inode);
  4254. entry = IFIRST(header);
  4255. /* No extended attributes present */
  4256. if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
  4257. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4258. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4259. new_extra_isize);
  4260. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4261. return 0;
  4262. }
  4263. /* try to expand with EAs present */
  4264. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4265. raw_inode, handle);
  4266. }
  4267. /*
  4268. * What we do here is to mark the in-core inode as clean with respect to inode
  4269. * dirtiness (it may still be data-dirty).
  4270. * This means that the in-core inode may be reaped by prune_icache
  4271. * without having to perform any I/O. This is a very good thing,
  4272. * because *any* task may call prune_icache - even ones which
  4273. * have a transaction open against a different journal.
  4274. *
  4275. * Is this cheating? Not really. Sure, we haven't written the
  4276. * inode out, but prune_icache isn't a user-visible syncing function.
  4277. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4278. * we start and wait on commits.
  4279. *
  4280. * Is this efficient/effective? Well, we're being nice to the system
  4281. * by cleaning up our inodes proactively so they can be reaped
  4282. * without I/O. But we are potentially leaving up to five seconds'
  4283. * worth of inodes floating about which prune_icache wants us to
  4284. * write out. One way to fix that would be to get prune_icache()
  4285. * to do a write_super() to free up some memory. It has the desired
  4286. * effect.
  4287. */
  4288. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4289. {
  4290. struct ext4_iloc iloc;
  4291. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4292. static unsigned int mnt_count;
  4293. int err, ret;
  4294. might_sleep();
  4295. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4296. if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4297. !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
  4298. /*
  4299. * We need extra buffer credits since we may write into EA block
  4300. * with this same handle. If journal_extend fails, then it will
  4301. * only result in a minor loss of functionality for that inode.
  4302. * If this is felt to be critical, then e2fsck should be run to
  4303. * force a large enough s_min_extra_isize.
  4304. */
  4305. if ((jbd2_journal_extend(handle,
  4306. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4307. ret = ext4_expand_extra_isize(inode,
  4308. sbi->s_want_extra_isize,
  4309. iloc, handle);
  4310. if (ret) {
  4311. EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
  4312. if (mnt_count !=
  4313. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4314. ext4_warning(inode->i_sb, __func__,
  4315. "Unable to expand inode %lu. Delete"
  4316. " some EAs or run e2fsck.",
  4317. inode->i_ino);
  4318. mnt_count =
  4319. le16_to_cpu(sbi->s_es->s_mnt_count);
  4320. }
  4321. }
  4322. }
  4323. }
  4324. if (!err)
  4325. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  4326. return err;
  4327. }
  4328. /*
  4329. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4330. *
  4331. * We're really interested in the case where a file is being extended.
  4332. * i_size has been changed by generic_commit_write() and we thus need
  4333. * to include the updated inode in the current transaction.
  4334. *
  4335. * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
  4336. * are allocated to the file.
  4337. *
  4338. * If the inode is marked synchronous, we don't honour that here - doing
  4339. * so would cause a commit on atime updates, which we don't bother doing.
  4340. * We handle synchronous inodes at the highest possible level.
  4341. */
  4342. void ext4_dirty_inode(struct inode *inode)
  4343. {
  4344. handle_t *current_handle = ext4_journal_current_handle();
  4345. handle_t *handle;
  4346. handle = ext4_journal_start(inode, 2);
  4347. if (IS_ERR(handle))
  4348. goto out;
  4349. if (current_handle &&
  4350. current_handle->h_transaction != handle->h_transaction) {
  4351. /* This task has a transaction open against a different fs */
  4352. printk(KERN_EMERG "%s: transactions do not match!\n",
  4353. __func__);
  4354. } else {
  4355. jbd_debug(5, "marking dirty. outer handle=%p\n",
  4356. current_handle);
  4357. ext4_mark_inode_dirty(handle, inode);
  4358. }
  4359. ext4_journal_stop(handle);
  4360. out:
  4361. return;
  4362. }
  4363. #if 0
  4364. /*
  4365. * Bind an inode's backing buffer_head into this transaction, to prevent
  4366. * it from being flushed to disk early. Unlike
  4367. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4368. * returns no iloc structure, so the caller needs to repeat the iloc
  4369. * lookup to mark the inode dirty later.
  4370. */
  4371. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4372. {
  4373. struct ext4_iloc iloc;
  4374. int err = 0;
  4375. if (handle) {
  4376. err = ext4_get_inode_loc(inode, &iloc);
  4377. if (!err) {
  4378. BUFFER_TRACE(iloc.bh, "get_write_access");
  4379. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4380. if (!err)
  4381. err = ext4_journal_dirty_metadata(handle,
  4382. iloc.bh);
  4383. brelse(iloc.bh);
  4384. }
  4385. }
  4386. ext4_std_error(inode->i_sb, err);
  4387. return err;
  4388. }
  4389. #endif
  4390. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4391. {
  4392. journal_t *journal;
  4393. handle_t *handle;
  4394. int err;
  4395. /*
  4396. * We have to be very careful here: changing a data block's
  4397. * journaling status dynamically is dangerous. If we write a
  4398. * data block to the journal, change the status and then delete
  4399. * that block, we risk forgetting to revoke the old log record
  4400. * from the journal and so a subsequent replay can corrupt data.
  4401. * So, first we make sure that the journal is empty and that
  4402. * nobody is changing anything.
  4403. */
  4404. journal = EXT4_JOURNAL(inode);
  4405. if (is_journal_aborted(journal))
  4406. return -EROFS;
  4407. jbd2_journal_lock_updates(journal);
  4408. jbd2_journal_flush(journal);
  4409. /*
  4410. * OK, there are no updates running now, and all cached data is
  4411. * synced to disk. We are now in a completely consistent state
  4412. * which doesn't have anything in the journal, and we know that
  4413. * no filesystem updates are running, so it is safe to modify
  4414. * the inode's in-core data-journaling state flag now.
  4415. */
  4416. if (val)
  4417. EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
  4418. else
  4419. EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
  4420. ext4_set_aops(inode);
  4421. jbd2_journal_unlock_updates(journal);
  4422. /* Finally we can mark the inode as dirty. */
  4423. handle = ext4_journal_start(inode, 1);
  4424. if (IS_ERR(handle))
  4425. return PTR_ERR(handle);
  4426. err = ext4_mark_inode_dirty(handle, inode);
  4427. handle->h_sync = 1;
  4428. ext4_journal_stop(handle);
  4429. ext4_std_error(inode->i_sb, err);
  4430. return err;
  4431. }
  4432. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  4433. {
  4434. return !buffer_mapped(bh);
  4435. }
  4436. int ext4_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  4437. {
  4438. loff_t size;
  4439. unsigned long len;
  4440. int ret = -EINVAL;
  4441. void *fsdata;
  4442. struct file *file = vma->vm_file;
  4443. struct inode *inode = file->f_path.dentry->d_inode;
  4444. struct address_space *mapping = inode->i_mapping;
  4445. /*
  4446. * Get i_alloc_sem to stop truncates messing with the inode. We cannot
  4447. * get i_mutex because we are already holding mmap_sem.
  4448. */
  4449. down_read(&inode->i_alloc_sem);
  4450. size = i_size_read(inode);
  4451. if (page->mapping != mapping || size <= page_offset(page)
  4452. || !PageUptodate(page)) {
  4453. /* page got truncated from under us? */
  4454. goto out_unlock;
  4455. }
  4456. ret = 0;
  4457. if (PageMappedToDisk(page))
  4458. goto out_unlock;
  4459. if (page->index == size >> PAGE_CACHE_SHIFT)
  4460. len = size & ~PAGE_CACHE_MASK;
  4461. else
  4462. len = PAGE_CACHE_SIZE;
  4463. if (page_has_buffers(page)) {
  4464. /* return if we have all the buffers mapped */
  4465. if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
  4466. ext4_bh_unmapped))
  4467. goto out_unlock;
  4468. }
  4469. /*
  4470. * OK, we need to fill the hole... Do write_begin write_end
  4471. * to do block allocation/reservation.We are not holding
  4472. * inode.i__mutex here. That allow * parallel write_begin,
  4473. * write_end call. lock_page prevent this from happening
  4474. * on the same page though
  4475. */
  4476. ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
  4477. len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  4478. if (ret < 0)
  4479. goto out_unlock;
  4480. ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
  4481. len, len, page, fsdata);
  4482. if (ret < 0)
  4483. goto out_unlock;
  4484. ret = 0;
  4485. out_unlock:
  4486. up_read(&inode->i_alloc_sem);
  4487. return ret;
  4488. }