inode.c 140 KB

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