inode.c 147 KB

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