inode.c 183 KB

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