inode.c 169 KB

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