inode.c 151 KB

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