inode.c 149 KB

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