inode.c 151 KB

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