inode.c 155 KB

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