inode.c 153 KB

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