inode.c 178 KB

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