inode.c 146 KB

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