inode.c 151 KB

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