inode.c 140 KB

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