inode.c 151 KB

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