inode.c 154 KB

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