inode.c 152 KB

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