inode.c 156 KB

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