inode.c 145 KB

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